Merge pull request #583 from xpluto/modify-leetcode-347
edit leetcode 347
This commit is contained in:
commit
9a794b889c
|
@ -404,7 +404,11 @@ public List<Integer> topKFrequent(int[] nums, int k) {
|
||||||
List<Integer> topK = new ArrayList<>();
|
List<Integer> topK = new ArrayList<>();
|
||||||
for (int i = buckets.length - 1; i >= 0 && topK.size() < k; i--) {
|
for (int i = buckets.length - 1; i >= 0 && topK.size() < k; i--) {
|
||||||
if (buckets[i] != null) {
|
if (buckets[i] != null) {
|
||||||
|
if (buckets[i].size() <= (k - topK.size())) {
|
||||||
topK.addAll(buckets[i]);
|
topK.addAll(buckets[i]);
|
||||||
|
} else {
|
||||||
|
topK.addAll(buckets[i].subList(0, k - topK.size()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return topK;
|
return topK;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user