auto commit

This commit is contained in:
CyC2018 2020-11-01 16:16:33 +08:00
parent 7b755d9910
commit 5ef1a5c4b3
2 changed files with 12 additions and 4 deletions

View File

@ -119,7 +119,7 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2].
把数都放到桶之后从后向前遍历桶最先得到的 k 个数就是出现频率最多的的 k 个数 把数都放到桶之后从后向前遍历桶最先得到的 k 个数就是出现频率最多的的 k 个数
```java ```java
public List<Integer> topKFrequent(int[] nums, int k) { public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> frequencyForNum = new HashMap<>(); Map<Integer, Integer> frequencyForNum = new HashMap<>();
for (int num : nums) { for (int num : nums) {
frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1); frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1);
@ -143,7 +143,11 @@ public List<Integer> topKFrequent(int[] nums, int k) {
topK.addAll(buckets[i].subList(0, k - topK.size())); topK.addAll(buckets[i].subList(0, k - topK.size()));
} }
} }
return topK; int[] res = new int[k];
for (int i = 0; i < k; i++) {
res[i] = topK.get(i);
}
return res;
} }
``` ```

View File

@ -119,7 +119,7 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2].
把数都放到桶之后从后向前遍历桶最先得到的 k 个数就是出现频率最多的的 k 个数 把数都放到桶之后从后向前遍历桶最先得到的 k 个数就是出现频率最多的的 k 个数
```java ```java
public List<Integer> topKFrequent(int[] nums, int k) { public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> frequencyForNum = new HashMap<>(); Map<Integer, Integer> frequencyForNum = new HashMap<>();
for (int num : nums) { for (int num : nums) {
frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1); frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1);
@ -143,7 +143,11 @@ public List<Integer> topKFrequent(int[] nums, int k) {
topK.addAll(buckets[i].subList(0, k - topK.size())); topK.addAll(buckets[i].subList(0, k - topK.size()));
} }
} }
return topK; int[] res = new int[k];
for (int i = 0; i < k; i++) {
res[i] = topK.get(i);
}
return res;
} }
``` ```