From 9b22f5e6afcaa66400e06e6862860068994161b9 Mon Sep 17 00:00:00 2001 From: xPluto Date: Tue, 26 Feb 2019 02:37:49 +0800 Subject: [PATCH] edit leetcode 347 --- docs/notes/Leetcode 题解.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/notes/Leetcode 题解.md b/docs/notes/Leetcode 题解.md index 7687a4d4..21a311f4 100644 --- a/docs/notes/Leetcode 题解.md +++ b/docs/notes/Leetcode 题解.md @@ -404,7 +404,11 @@ public List topKFrequent(int[] nums, int k) { List topK = new ArrayList<>(); for (int i = buckets.length - 1; i >= 0 && topK.size() < k; i--) { if (buckets[i] != null) { - topK.addAll(buckets[i]); + if (buckets[i].size() <= (k - topK.size())) { + topK.addAll(buckets[i]); + } else { + topK.addAll(buckets[i].subList(0, k - topK.size())); + } } } return topK;