当前位置:网站首页>代码随想录笔记_哈希_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;
}
边栏推荐
- Basic operations of MySQL database (I) --- Based on Database
- Leetcode - find the median of two positively ordered arrays
- R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
- 至少42名员工感染新冠病毒!诺基亚宣布关闭印度电信设备工厂
- One year anniversary of creation, Chongba young Lang
- Build Release Blogs
- Jerry's PWM setting and PWM IO selection [chapter]
- MySQL limit usage and large paging problem solving
- 0-1背包问题
- Network device hard core technology insider firewall and security gateway (10)
猜你喜欢

Maximize activation

Basic operations of MySQL database (I) --- Based on Database

Iperf installation and use

Database daily question --- day 22: last login

Read cmake in one article

福特SUV版“野马”正式下线,安全、舒适一个不落

共创文旅新篇章|新起典与国华文旅签订战略合作协议
![Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]](/img/e3/ea7278eb855cca2a1af4f96772bcb1.png)
Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]

芯片行业常用英文术语最详细总结(图文快速掌握)

Rongyun IM & RTC capabilities on new sites
随机推荐
592. Fraction addition and subtraction: introduction to expression calculation
The Canadian court found Meng Wanzhou guilty of "dual criminality", and the extradition procedure will continue!
Jerry's Bluetooth can only link back to the last device [article]
The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer
DC motor winding parameters
Arm发布全新A78/G78/N78内核!还有支持自定义的Cortex-X系列CPU
Function related knowledge
Scrollview, tableview nested solutions
Add a picture in front of the cell
为华为打造无美系设备的产线,台积电三星能做到吗?
Redis learning and understanding of three special data types
接口测试实战项目02:读懂接口测试文档,上手操练
0-1 knapsack problem
R language uses ggplot2 visualization: use ggpattern package to add custom stripe patterns, shadows, stripes, or other patterns or textures to the grouped bar graph
Data analysis: disassembly method (details)
Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
2020年一季度可穿戴市场出货量达7260万部,苹果独占近三成市场份额
When Jerry made a phone call, recording to SD card /u disk was not enough [article]
startUMl