当前位置:网站首页>Daily question brushing record (24)
Daily question brushing record (24)
2022-07-18 04:27:00 【Unique Hami melon】
List of articles
- The first question is : 2206. Divide the array into equal pairs
- The second question is : 2215. Find out the difference between the two arrays
- Third question : 2225. Find the player who lost zero or one game
- Fourth question : 2273. Result array after removing letter ectopic words
- Fifth question : 2283. Determine whether the number count of a number is equal to the value of the digit
- Sixth question : 2295. Replace elements in an array
The first question is : 2206. Divide the array into equal pairs
LeetCode: 2206. Divide the array into equal pairs
describe :
Give you an array of integers nums , It contains 2 * n It's an integer .
You need to nums Divide into n Pairs of numbers , Satisfy :
- Every element It belongs to only one Number pair .
- Elements in the same number pair equal .
If you can nums Divide into n Pairs of numbers , Please return true , Otherwise return to false .
Their thinking :
- From the title, we can see , The elements in the array appear in pairs ,
- If there is a single number of elements, it must be wrong .
- Here we use hash table , Traversal array , If the current element does not exist , Just add it directly to the hash table , If the current element exists , Just delete the element .
- Such a solution , Let every time there is 2 You can delete . The deleted ones are in pairs .
- If the last hash table is not empty , It means false
Code implementation :
class Solution {
public boolean divideArray(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num : nums) {
if(!set.add(num)){
set.remove(num);
}
}
return set.isEmpty();
}
}
The second question is : 2215. Find out the difference between the two arrays
LeetCode: 2215. Find out the difference between the two arrays
describe :
Give you two subscripts from 0 The starting array of integers nums1 and nums2 , Please return a length of 2 A list of answer , among :
answer[0]yesnums1All in No Exist innums2Medium Different List of integers .answer[1]yesnums2All in No Exist innums1Medium Different List of integers .
Be careful : The integers in the list can be pressed arbitrarily Sequential return .
Their thinking :
- Create two hash tables , Will array
nums1, Add to a hash tableset1. Will arraynums2, Add to another hash tableset2- Traverse the hash table again
set1, Look at the hash table 1 Whether there is a hash table for the element in 2 in , If the hash table 2 Hash table does not exist in 1 The elements in , It means that this is a redundant element , Put it in the result set .- Traverse the hash table again
set2, Look at the hash table 2 Whether there is a hash table for the element in 1 in , If the hash table 1 Hash table does not exist in 2 The elements in , It means that this is a redundant element , Also put into the result set ,
Code implementation :
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for(int num : nums1) set1.add(num);
for(int num : nums2) set2.add(num);
List<List<Integer>> res = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
for(int num : set1) {
if(!set2.contains(num)){
list1.add(num);
}
}
for(int num : set2) {
if(!set1.contains(num)){
list2.add(num);
}
}
res.add(new ArrayList<>(list1));
res.add(new ArrayList<>(list2));
return res;
}
}
Third question : 2225. Find the player who lost zero or one game
LeetCode: 2225. Find the player who lost zero or one game
describe :
Give you an array of integers matches among matches[i] = [winneri, loseri] In a game winneri Beat the loseri .
Returns a length of 2 A list of answer :
answer[0]It's all No, List of players who lost any game .answer[1]Is all that happens to lose a List of players in the game .
The values in both lists should be pressed Increasing Sequential return .
Be careful :
- Only those involved At least one The players of the game .
- The generated test cases guarantee non-existent The results of two games identical .

Their thinking :
- Here we use hash table to solve problems
- Traversal array , If it's for
matches[i][0]Subscript elements are added directly ,valueValue is set to 0. If it's formatches[i][1]Subscript element pairsvalueValue for +1 operation ,- If
keyCorrespondingvalueThe value is 0, It is a failed addition to the result set , IfkeyCorrespondingvalueThe value is 1, It's a player who only loses one , Add to result set .- Be careful to return in ascending order . Sort .
Code implementation :
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < matches.length; i++) {
if(!map.containsKey(matches[i][0]))map.put(matches[i][0],0);
map.put(matches[i][1],map.getOrDefault(matches[i][1],0)+1);
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
for(int key : map.keySet()) {
if(map.get(key) == 0) {
list1.add(key);
}else if(map.get(key) == 1){
list2.add(key);
}
}
Collections.sort(list1);
Collections.sort(list2);
res.add(new ArrayList<>(list1));
res.add(new ArrayList<>(list2));
return res;
}
}
Fourth question : 2273. Result array after removing letter ectopic words
LeetCode: 2273. Result array after removing letter ectopic words
describe :
I'll give you a subscript from 0 Starting string words , among words[i] Composed of lowercase English characters .
In one step , Any subscript needs to be selected i , from words in Delete words[i] . Subscript i The following two conditions need to be met at the same time :
0 < i < words.lengthwords[i - 1]andwords[i]yes Letter heterotopic word .
As long as you can choose a subscript that meets the conditions , Just keep doing this .
After performing all operations , return words . Can prove that , Selecting subscripts for each operation in any order will get the same result .
Letter heterotopic word Is a new word obtained by rearranging the letters of the source word , Letters in all source words are usually used just once . for example ,"dacb" yes "abdc" A letter heterotopic word of .
Their thinking :
- Rearrange each character in the string array
- Go to pairs and compare /
- If it is consistent, add it only once
- If it is inconsistent, add it directly
Code implementation :
class Solution {
public List<String> removeAnagrams(String[] words) {
List<String> res = new ArrayList<>();
String pre = "";
for(String word : words) {
char[] ch = word.toCharArray();
Arrays.sort(ch);
String newWord = new String(ch);
if(!pre.equals(newWord)) {
res.add(word);
pre = newWord;
}
}
return res;
}
}
Fifth question : 2283. Determine whether the number count of a number is equal to the value of the digit
LeetCode: 2283. Determine whether the number count of a number is equal to the value of the digit
describe :
I'll give you a subscript from 0 Start length is n String num , It contains only numbers .
If for Every 0 <= i < n The subscript i , All meet the digit i stay num In the num[i] Time , So please go back to true , Otherwise return to false .
Their thinking :
- Traversal string
num, Record each character , Record the number of times the corresponding number appears .- Traverse... Again , Comparison correspondence i Number of subscripts , Whether it is consistent with the current number of times represented by the string , Inconsistent direct return false
- If the traversal ends , Just go back to true
Code implementation :
class Solution {
public boolean digitCount(String num) {
int[] arr = new int[10];
for(char ch : num.toCharArray()) {
arr[ch-'0']++;
}
for(int i = 0; i < num.length();i++){
if(arr[i] != num.charAt(i)-'0'){
return false;
}
}
return true;
}
}
Sixth question : 2295. Replace elements in an array
LeetCode: 2295. Replace elements in an array
describe :
I'll give you a subscript from 0 Starting array nums , It contains n individual Different from each other The positive integer . Please execute on this array m Operations , In the i In one operation , You need to put the numbers operations[i][0] Replace with operations[i][1] .
The title is guaranteed in i In one operation :
operations[i][0]staynumsin .operations[i][1]staynumsDoes not exist in the .
Please return the array after performing all operations .

Their thinking :
- Solve problems with hash table
- Traversal array
nums, Put each element , And subscripts are recorded in the hash table .- Re traversal
operations, If at presentoperations[i][0]The subscript of exists in the hash table , Get subscript , Then replace the array elements withoperations[i][1], Then put the updated elements and subscripts into the hash table .- End of traversal . The array has been modified
Code implementation :
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
map.put(nums[i],i);
}
for(int i = 0; i < operations.length; i++) {
if(map.containsKey(operations[i][0])) {
int index = map.get(operations[i][0]);
nums[index] = operations[i][1];
map.put(nums[index],index);
}
}
return nums;
}
}
边栏推荐
- 【OpenFOAM學前預備3——安裝OpenFOAM-v8】
- MySQL8.0学习记录18 - Tablespaces
- 技术分享 | 常见接口协议解析
- 备忘录模式 - Unity
- 正则表达式练习
- SaaS application: the best way to realize enterprise digital transformation
- Among the top 50 intelligent operation and maintenance enterprises in 2022, Borui data strength was selected
- Scroll view fixed height to achieve bottom touch effect
- 现在网上开户安全么?接着证券开户选择哪个证券
- mysql 传入List<String>进行查询的方法
猜你喜欢

ThreadLocal原理及源码解析(一步一步点进去,不要背了,学思想)
Gaussdb (DWS), the first benchmarking MySQL command collection article in the whole network
The practice of opengauss database on CentOS, configuration

如何使用Fiddler进行弱网测试

40+倍提升,详解 JuiceFS 元数据备份恢复性能优化之路

Tinymce5.0.8 editor latest version Chinese version

模糊测试的简介

【走進go的內心深處】

StarRocks 社区架构出炉,等你通关升级!

101.(cesium篇)cesium粒子系统-下雪
随机推荐
Technology sharing | packet capturing analysis TCP protocol
初识ESP8266(一)————接入点与无线终端模式
技术分享 | 使用postman发送请求
DPU — 完全可编程网络
Gaussdb (DWS), the first benchmarking MySQL command collection article in the whole network
The relationship between reinforcement learning (Q-learning) and path search (a*)
技术分享 | 抓包分析 TCP 协议
Neural network loss and ACC drawing method plot
高性能算力中心 — RDMA — NVIDIA SHARP
扁平化骑手注册表单
Creative ribbon style landing page
JS image editor plug-in filerobot
Flock's yarn clustering mode (1)
Blue Bridge Cup VIP question bank
最大子段和+线段树.1
使用 Terraform 在阿里云上快速部署 MQTT 集群
从源码学习线程池的使用原理及核心思想解析
基于.NET动态编译技术实现任意代码执行
Is it safe for qiniu business school to open an account? Is it reliable? How can qiniu open an account
flink的测试sql怎么测试呢,不能每次都使用jar测试吧,那么sqk-client就来了