Update Leetcode 题解.md

This commit is contained in:
CyC2018 2019-02-26 15:14:52 +08:00
commit cc9dd446f2

View File

@ -403,10 +403,12 @@ 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].size() <= (k - topK.size())) { if (buckets[i] != null) {
topK.addAll(buckets[i]); if (buckets[i].size() <= (k - topK.size())) {
} else { topK.addAll(buckets[i]);
topK.addAll(buckets[i].subList(0, k - topK.size())); } else {
topK.addAll(buckets[i].subList(0, k - topK.size()));
}
} }
} }
return topK; return topK;