添加 1002.查找常用字符python3版本

This commit is contained in:
ironartisan
2021-08-07 10:25:15 +08:00
parent 5c3d670480
commit 3caaf866f4

View File

@ -169,6 +169,29 @@ class Solution {
}
}
```
```python
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
if not words: return []
result = []
hash = [0] * 26 # 用来统计所有字符串里字符出现的最小频率
for i, c in enumerate(words[0]): # 用第一个字符串给hash初始化
hash[ord(c) - ord('a')] += 1
# 统计除第一个字符串外字符的出现频率
for i in range(1, len(words)):
hashOtherStr = [0] * 26
for j in range(len(words[0])):
hashOtherStr[ord(words[i][j]) - ord('a')] += 1
# 更新hash保证hash里统计26个字符在所有字符串里出现的最小次数
for k in range(26):
hash[k] = min(hash[k], hashOtherStr[k])
# 将hash统计的字符次数转成输出形式
for i in range(26):
while hash[i] != 0: # 注意这里是while多个重复的字符
result.extend(chr(i + ord('a')))
hash[i] -= 1
return result
```
javaScript
```js
var commonChars = function (words) {