mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #574 from ironartisan/master
添加 1002.查找常用字符python3版本
This commit is contained in:
@ -127,7 +127,7 @@ class Solution:
|
|||||||
for num in nums2:
|
for num in nums2:
|
||||||
if num in set1:
|
if num in set1:
|
||||||
result_set.add(num) # set1里出现的nums2元素 存放到结果
|
result_set.add(num) # set1里出现的nums2元素 存放到结果
|
||||||
return result_set
|
return list(result_set)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
javaScript
|
||||||
```js
|
```js
|
||||||
var commonChars = function (words) {
|
var commonChars = function (words) {
|
||||||
|
@ -100,7 +100,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
Reference in New Issue
Block a user