Skip to content

Commit

Permalink
添加 1002.查找常用字符.md Python3 使用collections.Counter实现
Browse files Browse the repository at this point in the history
  • Loading branch information
shuwenlan authored Sep 3, 2021
1 parent b8ef037 commit bcd0397
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions problems/1002.查找常用字符.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@ class Solution:
hash[i] -= 1
return result
```

Python 3 使用collections.Counter
```python
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
tmp = collections.Counter(words[0])
l = []
for i in range(1,len(words)):
# 使用 & 取交集
tmp = tmp & collections.Counter(words[i])

# 剩下的就是每个单词都出现的字符(键),个数(值)
for j in tmp:
v = tmp[j]
while(v):
l.append(j)
v -= 1
return l
```

javaScript
```js
var commonChars = function (words) {
Expand Down

0 comments on commit bcd0397

Please sign in to comment.