当前位置:网站首页>代码随想录笔记_哈希_1002查找共用字符
代码随想录笔记_哈希_1002查找共用字符
2022-07-27 22:29:00 【Erik_Won】
代码随想录笔记_哈希表
代码随想录二刷笔记记录
LC1002.查找共用字符
题目
给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案。
示例 1:
输入:words = [“bella”,“label”,“roller”]
输出:[“e”,“l”,“l”]
示例 2:
输入:words = [“cool”,“lock”,“cook”]
输出:[“c”,“o”]
思路分析
- 题意解析:从26个小写字符中,有字符(a)在所有的字符串 (aapply/aapple/aaugment)里都出现的话,就输出,重复的也算,即输出[‘a’,‘a’],b,c,d,…z 同理
- 思路:哈希数组,记录每个字符串的哈希值,取最小值
- 推演分析:哈希表统计 words 中 26 个字符出现的频率,然后取每个字符串的最小值,再转换为字符(String)输出

a b e l o r z
bella: [1 1 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
label: [1 1 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
roller: [0 0 0 0 1 0 0 0 0 0 0 2 0 0 1 0 0 2 0 0 0 0 0 0 0 0 ]
Math.min: [0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
得到输出["e","l","l"]
代码实现
完整代码实现
public List<String> commonChars(String[] words) {
List<String> res = new ArrayList<>();
if(words.length == 0) return res;
//扫描第一个String,初始化 hash
int[] record = new int[26]; // 记录第 1 个String的 Hash
for (int i = 0; i < words[0].length();i++){
record[words[0].charAt(i) - 'a']++;
}
//扫描之后的 String
for (int i = 1; i < words.length;i++){
int[] temp = new int[26]; // 记录第 i 个 String 的 Hash
for (int j = 0; j < words[i].length();j++){
temp[words[i].charAt(j) - 'a']++;
}
//对比 第 i 个 String 和第 i-1 个 String 的 Hash
for(int k = 0; k < record.length;k++){
record[k] = Math.min(record[k],temp[k]);取 Hash 最小值
}
}
//转换为 String
for (int i = 0;i < record.length;i++) {
while (record[i] > 0){
//因为有重复,所以需要 while
char c = (char)(i+'a');
res.add(String.valueOf(c));
record[i]--;
}
}
return res;
}
边栏推荐
- Logic of automatic reasoning 09 - automatic theorem proving
- Network equipment hard core technology insider firewall and security gateway (12) the mystery of zero contact office
- Basic operations of MySQL database (3) --- Based on fields
- Buildforge materials
- Data visualization - White Snake 2: black snake robbery (3)
- Process and process scheduling
- Interesting Huffman tree
- Byte flybook Human Resource Kit three sides
- Syntaxerror resolved: positive argument follows keyword argument
- Build Release Blogs
猜你喜欢

Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[BuildRelease Management]Parabuild

Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore

Maximize activation

多线程及多线程程序的编写

IP address & subnet mask

数组相关知识

Confused SCM matrix keys

红队大杀器 Behinder_v4.0(冰蝎4.0)

融云 IM & RTC 能力上新盘点
随机推荐
Data visualization - White Snake 2: black snake robbery (3)
Logic of automatic reasoning 09 - automatic theorem proving
Maximize activation
芯片行业常用英文术语最详细总结(图文快速掌握)
592. Fraction addition and subtraction: introduction to expression calculation
Byte flybook Human Resource Kit three sides
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
Code review tool
Rendering problems
[bre] software build release automation
Count the six weapons of the domestic interface cooperation platform!
Leetcode - find the median of two positively ordered arrays
Jerry caused other messages to accumulate in the message pool [article]
至少42名员工感染新冠病毒!诺基亚宣布关闭印度电信设备工厂
ASML launched the first generation HMI multi beam detector: the speed is increased by 600%, which is suitable for 5nm and more advanced processes
Jerry Zhi has problems in real-time adjustment of sound change effect in magic sound mode [chapter]
At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
Leetcode 415. string addition and 43. string multiplication
数组相关知识
Volkswagen China invested 8billion yuan and became the largest shareholder of GuoXuan high tech