mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
1002.查找常用字符:优化排版,修改错别字
This commit is contained in:
@ -58,7 +58,7 @@ words[i] 由小写英文字母组成
|
||||
|
||||
先统计第一个字符串所有字符出现的次数,代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
int hash[26] = {0}; // 用来统计所有字符串里字符出现的最小频率
|
||||
for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化
|
||||
hash[A[0][i] - 'a']++;
|
||||
@ -71,7 +71,7 @@ for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
int hashOtherStr[26] = {0}; // 统计除第一个字符串外字符的出现频率
|
||||
for (int i = 1; i < A.size(); i++) {
|
||||
memset(hashOtherStr, 0, 26 * sizeof(int));
|
||||
@ -84,11 +84,11 @@ for (int i = 1; i < A.size(); i++) {
|
||||
}
|
||||
}
|
||||
```
|
||||
此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转正题目要求的输出格式就可以了。
|
||||
此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转成题目要求的输出格式就可以了。
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
// 将hash统计的字符次数,转成输出形式
|
||||
for (int i = 0; i < 26; i++) {
|
||||
while (hash[i] != 0) { // 注意这里是while,多个重复的字符
|
||||
|
Reference in New Issue
Block a user