Skip to content

Commit d059b3f

Browse files
committed
feat: add solutions to lc problem: No.0347. Top K Frequent Elements
* add solutions to No.0347 * update python counter
1 parent b4daf1d commit d059b3f

File tree

19 files changed

+157
-74
lines changed

19 files changed

+157
-74
lines changed

lcci/01.04.Palindrome Permutation/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
```python
3737
class Solution:
3838
def canPermutePalindrome(self, s: str) -> bool:
39-
counter = collections.Counter()
40-
for c in s:
41-
counter[c] += 1
39+
counter = collections.Counter(s)
4240
cnt = 0
4341
for val in counter.values():
4442
if (val & 1) == 1:

lcci/01.04.Palindrome Permutation/README_EN.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
```python
2828
class Solution:
2929
def canPermutePalindrome(self, s: str) -> bool:
30-
counter = collections.Counter()
31-
for c in s:
32-
counter[c] += 1
30+
counter = collections.Counter(s)
3331
cnt = 0
3432
for val in counter.values():
3533
if (val & 1) == 1:

lcci/01.04.Palindrome Permutation/Solution.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Solution:
22
def canPermutePalindrome(self, s: str) -> bool:
3-
counter = collections.Counter()
4-
for c in s:
5-
counter[c] += 1
3+
counter = collections.Counter(s)
64
cnt = 0
75
for val in counter.values():
86
if (val & 1) == 1:

lcof/面试题50. 第一个只出现一次的字符/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ s = ""
3131
### **Python3**
3232

3333
```python
34-
import collections
35-
3634
class Solution:
3735
def firstUniqChar(self, s: str) -> str:
38-
counter = collections.Counter()
39-
for c in s:
40-
counter[c] += 1
36+
counter = collections.Counter(s)
4137
for c in s:
4238
if counter[c] == 1:
4339
return c
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import collections
2-
31
class Solution:
42
def firstUniqChar(self, s: str) -> str:
5-
counter = collections.Counter()
6-
for c in s:
7-
counter[c] += 1
3+
counter = collections.Counter(s)
84
for c in s:
95
if counter[c] == 1:
106
return c
11-
return ' '
7+
return ' '

solution/0300-0399/0347.Top K Frequent Elements/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,67 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
“桶排序”实现。
46+
4547
<!-- tabs:start -->
4648

4749
### **Python3**
4850

4951
<!-- 这里可写当前语言的特殊实现逻辑 -->
5052

5153
```python
52-
54+
class Solution:
55+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
56+
counter = collections.Counter(nums)
57+
buckets = [[] for _ in range(len(nums) + 1)]
58+
for num, freq in counter.items():
59+
buckets[freq].append(num)
60+
i, res = len(nums), []
61+
while k > 0 and i >= 0:
62+
if buckets[i]:
63+
for num in buckets[i]:
64+
if k <= 0:
65+
break
66+
res.append(num)
67+
k -= 1
68+
i -= 1
69+
return res
5370
```
5471

5572
### **Java**
5673

5774
<!-- 这里可写当前语言的特殊实现逻辑 -->
5875

5976
```java
60-
77+
class Solution {
78+
public int[] topKFrequent(int[] nums, int k) {
79+
Map<Integer, Integer> counter = new HashMap<>();
80+
for (int num : nums) {
81+
counter.put(num, counter.getOrDefault(num, 0) + 1);
82+
}
83+
List<Integer>[] buckets = new List[nums.length + 1];
84+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
85+
int num = entry.getKey();
86+
int freq = entry.getValue();
87+
if (buckets[freq] == null) {
88+
buckets[freq] = new ArrayList<>();
89+
}
90+
buckets[freq].add(num);
91+
}
92+
int[] res = new int[k];
93+
for (int i = nums.length; i >= 0 && k > 0; --i) {
94+
if (buckets[i] != null) {
95+
for (int num : buckets[i]) {
96+
if (k <= 0) {
97+
break;
98+
}
99+
res[--k] = num;
100+
}
101+
}
102+
}
103+
return res;
104+
}
105+
}
61106
```
62107

63108
### **...**

solution/0300-0399/0347.Top K Frequent Elements/README_EN.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,56 @@
3434
### **Python3**
3535

3636
```python
37-
37+
class Solution:
38+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
39+
counter = collections.Counter(nums)
40+
buckets = [[] for _ in range(len(nums) + 1)]
41+
for num, freq in counter.items():
42+
buckets[freq].append(num)
43+
i, res = len(nums), []
44+
while k > 0 and i >= 0:
45+
if buckets[i]:
46+
for num in buckets[i]:
47+
if k <= 0:
48+
break
49+
res.append(num)
50+
k -= 1
51+
i -= 1
52+
return res
3853
```
3954

4055
### **Java**
4156

4257
```java
43-
58+
class Solution {
59+
public int[] topKFrequent(int[] nums, int k) {
60+
Map<Integer, Integer> counter = new HashMap<>();
61+
for (int num : nums) {
62+
counter.put(num, counter.getOrDefault(num, 0) + 1);
63+
}
64+
List<Integer>[] buckets = new List[nums.length + 1];
65+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
66+
int num = entry.getKey();
67+
int freq = entry.getValue();
68+
if (buckets[freq] == null) {
69+
buckets[freq] = new ArrayList<>();
70+
}
71+
buckets[freq].add(num);
72+
}
73+
int[] res = new int[k];
74+
for (int i = nums.length; i >= 0 && k > 0; --i) {
75+
if (buckets[i] != null) {
76+
for (int num : buckets[i]) {
77+
if (k <= 0) {
78+
break;
79+
}
80+
res[--k] = num;
81+
}
82+
}
83+
}
84+
return res;
85+
}
86+
}
4487
```
4588

4689
### **...**
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
class Solution {
2-
public List<Integer> topKFrequent(int[] nums, int k) {
3-
Map<Integer, Integer> map = new HashMap<>();
2+
public int[] topKFrequent(int[] nums, int k) {
3+
Map<Integer, Integer> counter = new HashMap<>();
44
for (int num : nums) {
5-
map.put(num, map.getOrDefault(num, 0) + 1);
5+
counter.put(num, counter.getOrDefault(num, 0) + 1);
66
}
7-
List<Integer>[] buckets = new ArrayList[nums.length + 1];
8-
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
7+
List<Integer>[] buckets = new List[nums.length + 1];
8+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
99
int num = entry.getKey();
10-
int count = entry.getValue();
11-
if (buckets[count] == null) {
12-
buckets[count] = new ArrayList<>();
10+
int freq = entry.getValue();
11+
if (buckets[freq] == null) {
12+
buckets[freq] = new ArrayList<>();
1313
}
14-
buckets[count].add(num);
14+
buckets[freq].add(num);
1515
}
16-
List<Integer> topK = new ArrayList<>(k);
17-
for (int i = buckets.length - 1; i >= 0 && topK.size() < k; --i) {
16+
int[] res = new int[k];
17+
for (int i = nums.length; i >= 0 && k > 0; --i) {
1818
if (buckets[i] != null) {
19-
topK.addAll(buckets[i]);
19+
for (int num : buckets[i]) {
20+
if (k <= 0) {
21+
break;
22+
}
23+
res[--k] = num;
24+
}
2025
}
2126
}
22-
return topK;
27+
return res;
2328
}
24-
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
counter = collections.Counter(nums)
4+
buckets = [[] for _ in range(len(nums) + 1)]
5+
for num, freq in counter.items():
6+
buckets[freq].append(num)
7+
i, res = len(nums), []
8+
while k > 0 and i >= 0:
9+
if buckets[i]:
10+
for num in buckets[i]:
11+
if k <= 0:
12+
break
13+
res.append(num)
14+
k -= 1
15+
i -= 1
16+
return res

solution/0300-0399/0389.Find the Difference/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@
6464
```python
6565
class Solution:
6666
def findTheDifference(self, s: str, t: str) -> str:
67-
counter = collections.Counter()
68-
for c in s:
69-
counter[c] += 1
67+
counter = collections.Counter(s)
7068
for c in t:
7169
if counter[c] <= 0:
7270
return c

0 commit comments

Comments
 (0)