当前位置:网站首页>Li Kou brushing questions - hash table
Li Kou brushing questions - hash table
2022-06-11 02:21:00 【Xiao Wang who learns C language well】


Preface
Hello, friends , I'm your classmate Xiao Wang
Today's topic of force deduction —— Hashtable
Hope to bring you useful knowledge
What I think Xiao Wang wrote is good Please use your hands give the thumbs-up Collection Comment on
Xiao Wang's home page : Xiao Wang
Xiao Wang's gitee: Xiao Wang
Xiao Wang's github: Xiao Wang
Catalog

442 Title Description :
Give you a length of n Array of integers for nums , among nums All integers of are in the range [1, n] Inside , And each integer appears once or two . Please find out all that appear two The integer of , And Array form return .
You must design and implement a time complexity of O(n) And only the algorithm of constant extra space is used to solve this problem .
Example 1:
Input :nums = [4,3,2,7,8,2,3,1]
Output :[2,3]
Example 2:Input :nums = [1,1,2]
Output :[1]
Example 3:Input :nums = [1]
Output :[]
Their thinking :
According to the title requirements Use extra space to solve
We open up an array with equal space size
- Go through the array
- If there is a repeating element add To list in
- Finally, it will be returned as an array
Code attached :
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer>list =new ArrayList<>();
int arr[]=new int [nums.length];
for(int i=0;i<nums.length;i++){
if(nums[i]==arr[nums[i]-1]){
list.add(nums[i]);
}else{
arr[nums[i]-1]=nums[i];
}
}
return list;
}
}
2283 Title Description :
Give you a length of n Array of integers for nums , among nums All integers of are in the range [1, n] Inside , And each integer appears once or two . Please find out all that appear two The integer of , And return as an array .
You must design and implement a time complexity of O(n) And only the algorithm of constant extra space is used to solve this problem .
Example 1:
Input :nums = [4,3,2,7,8,2,3,1]
Output :[2,3]
Example 2:Input :nums = [1,1,2]
Output :[1]
Example 3:Input :nums = [1]
Output :[]
Their thinking :
This problem is directly related to the statistics frequency that will do
Traverse the number of occurrences of statistics
If the subscript i Content in num In the num[i] Time So return true Instead, return to false
Code attached :
class Solution {
public boolean digitCount(String num) {
int len=num.length();
int []map=new int [10];
for(int i=0;i<len;i++){
// The number of occurrences of Statistics
map[num.charAt(i)-'0']++;
}
for(int i=0;i<len;i++){
if(map[i]!=num.charAt(i)-'0') return false;
}
return true;
}
}
884 Title Description :
The sentence A string of words separated by a space . Every word It's only made up of lowercase letters .
If a word happens to appear once in one of the sentences , In another sentence There was no , So the word is Unusual .
- Here are two for you The sentence s1 and Unordered list s2 , Back to all Uncommon words A list of . To return to the words in the list, you can press In any order organization .
Example 1:
Input :s1 = "this apple is sweet", s2 = "this apple is sour"
Output :["sweet","sour"]
Example 2:Input :s1 = "apple apple", s2 = "banana"
Output :["banana"]
Their thinking :
- Create a string array to splice two strings
- use Hashtable The map counts the number of occurrences of each string
- Iterate over the hash table Set all values to 1 Key placement for
If the number of occurrences is 1 It is the only uncommon string mentioned in the title
Code details :
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
// Create a string array to concatenate the two strings
String []AB=(s1+" "+s2).split(" ");
// Count the number of occurrences of each string
Map<String,Integer>map=new HashMap<>();
for(int i=0;i<AB.length;i++){
map.put(AB[i],map.getOrDefault(AB[i],0)+1);
}
// If the number of occurrences is 1 It is the only uncommon string mentioned in the title
List<String>list=new ArrayList<>();
for(String key:map.keySet()){
if(map.get(key)==1){
list.add(key);
}
}
String []res=new String[list.size()];
return list.toArray(res);
}
}2068 Title Description :
If two strings word1 and word2 In the from 'a' To 'z' The difference in the frequency of each letter is No more than 3 , So we call these two strings word1 and word2 Almost equal .
Here you are. Both lengths are n String word1 and word2 , If word1 and word2 Almost equal , Please return true , Otherwise return to false .
A letter x Appearance frequency It refers to the number of times it appears in the string .
Example 1:
Input :word1 = "aaaa", word2 = "bccb"
Output :false
explain : character string "aaaa" There is 4 individual 'a' , however "bccb" There is 0 individual 'a' .
The difference between the two is 4 , Greater than upper limit 3 .
Example 2:Input :word1 = "abcdeef", word2 = "abaaacc"
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 :
- 'a' stay word1 In the 1 Time , stay word2 In the 4 Time , The difference is 3 .
- 'b' stay word1 In the 1 Time , stay word2 In the 1 Time , The difference is 0 .
- 'c' stay word1 In the 1 Time , stay word2 In the 2 Time , The difference is 1 .
- 'd' stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
- 'e' stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
- 'f' stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
Example 3:Input :word1 = "cccddabba", word2 = "babababab"
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 :
- 'a' stay word1 In the 2 Time , stay word2 In the 4 Time , The difference is 2 .
- 'b' stay word1 In the 2 Time , stay word2 In the 5 Time , The difference is 3 .
- 'c' stay word1 In the 3 Time , stay word2 In the 0 Time , The difference is 3 .
- 'd' stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
Their thinking :
- Create a 26 An array of size spaces To store 'a'-'z' The number of letters
- Traverse word1 and word2 Two strings
- The first string appears ++, The second one is --
- Finally, judge if the absolute value exceeds 3 Just go back to false Come back anyway true!
Code attached :
class Solution {
public boolean checkAlmostEquivalent(String word1, String word2) {
int res[]=new int[26]; // Statistical characters 'a' To character 'z' Number of occurrences
for(char c1:word1.toCharArray()){
res[c1-'a']++;
}
for(char c2:word2.toCharArray()){
res[c2-'a']--;
}
// for(int i=0;i<word1.length();i++){
// res[word1.charAt(i)-'a']++;
// }
// for(int i=0;i<word2.length();i++){
// res[word2.charAt(i)-'a']--;
// }
for(int i=0;i<26;i++){
if(Math.abs(res[i])>3){
return false;
}
}
return true;
}
} 
边栏推荐
- 889. 根据前序和后序遍历构造二叉树
- adb 常用命令解析
- Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
- Fb02 edit coding block field
- 10 years of domestic milk powder counter attack: post-90s nannies and dads help new domestic products counter attack foreign brands
- Secret
- JS basic part hand exercises
- 常见漏洞的防御措施整理
- Oracle tablespaces, users, and authorization to users
- English subtitle video translated into Chinese subtitles
猜你喜欢

The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?

金属有机骨架材料Fe-MIL-53,Mg-MOF-74,Ti-KUMOF-1,Fe-MIL-100,Fe-MIL-101)负载异氟醚/甲氨蝶呤/阿霉素(DOX)/紫杉醇/布洛芬/喜树碱

Ortele has obtained three rounds of financing nine months after its establishment, and hard discount stores have found new ways to grow?

Nodejs send mail

20n10-asemi medium and small power MOS transistor 20n10

Understanding of pointers

10 years of domestic milk powder counter attack: post-90s nannies and dads help new domestic products counter attack foreign brands

SAP smartforms page feed printing automatic judgment
![[3.delphi common components] 8 dialog box](/img/4b/c06f8789cee58705a0b77f3ca1eee6.jpg)
[3.delphi common components] 8 dialog box

QT database learning notes (II) QT operation SQLite database
随机推荐
[penetration test tool bee] how to install and use the XSS penetration test tool bee?
软件测试是否需要掌握编程能力
Using an old mobile phone to build a server and achieve intranet penetration does not require root (I have personally tested the simplest one many times)
渗透测试-安全服务体系+OWASP top 10
Fallible point--
During SSH configuration key login, you need to pay attention to whether the private key is set with a password
10007. ISBN号码
Question g: candy
CRS-5017
双面材质【double sided】的Shader
Md61 plan independent demand import Bapi [by daily dimension / dynamic template / dynamic field]
Tencent test development post interview programming questions
adb 常用命令解析
【无标题】
从测试零基础到测试架构师,这10年,他是这样让自己成才的
安全生产月知识竞赛——新安法知多少
Project records
Rewrite: kms activates office2016, 2019 and 2021 with error code: 0xc004f069
贵金属白银和现货白银之间是什么关系
Union find