Merge pull request #574 from ironartisan/master

添加 1002.查找常用字符python3版本
This commit is contained in:
程序员Carl
2021-08-08 15:32:34 +08:00
committed by GitHub
3 changed files with 39 additions and 2 deletions

View File

@ -127,7 +127,7 @@ class Solution:
for num in nums2:
if num in set1:
result_set.add(num) # set1里出现的nums2元素 存放到结果
return result_set
return list(result_set)
```

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) {

View File

@ -100,7 +100,21 @@ class Solution {
```
Python
```python
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
count = [0] * 2002
for i in range(len(arr)):
count[arr[i] + 1000] += 1 # 防止负数作为下标
freq = [False] * 1002 # 标记相同频率是否重复出现
for i in range(2001):
if count[i] > 0:
if freq[count[i]] == False:
freq[count[i]] = True
else:
return False
return True
```
Go
JavaScript