From 5ef1a5c4b3c2a35af4e3acfcc90a9a11b0eed7ed Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Sun, 1 Nov 2020 16:16:33 +0800 Subject: [PATCH] auto commit --- docs/notes/Leetcode 题解 - 排序.md | 8 ++++++-- notes/Leetcode 题解 - 排序.md | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/notes/Leetcode 题解 - 排序.md b/docs/notes/Leetcode 题解 - 排序.md index 09fdc70d..8129b06d 100644 --- a/docs/notes/Leetcode 题解 - 排序.md +++ b/docs/notes/Leetcode 题解 - 排序.md @@ -119,7 +119,7 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2]. 把数都放到桶之后,从后向前遍历桶,最先得到的 k 个数就是出现频率最多的的 k 个数。 ```java -public List topKFrequent(int[] nums, int k) { +public int[] topKFrequent(int[] nums, int k) { Map frequencyForNum = new HashMap<>(); for (int num : nums) { frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1); @@ -143,7 +143,11 @@ public List topKFrequent(int[] nums, int k) { 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; } ``` diff --git a/notes/Leetcode 题解 - 排序.md b/notes/Leetcode 题解 - 排序.md index 09fdc70d..8129b06d 100644 --- a/notes/Leetcode 题解 - 排序.md +++ b/notes/Leetcode 题解 - 排序.md @@ -119,7 +119,7 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2]. 把数都放到桶之后,从后向前遍历桶,最先得到的 k 个数就是出现频率最多的的 k 个数。 ```java -public List topKFrequent(int[] nums, int k) { +public int[] topKFrequent(int[] nums, int k) { Map frequencyForNum = new HashMap<>(); for (int num : nums) { frequencyForNum.put(num, frequencyForNum.getOrDefault(num, 0) + 1); @@ -143,7 +143,11 @@ public List topKFrequent(int[] nums, int k) { 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; } ```