Skip to content

Commit fce2b1f

Browse files
refactor 347
1 parent 9d1d779 commit fce2b1f

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/main/java/com/fishercoder/solutions/_347.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class Solution1 {
1515
* Use buckets to hold numbers of the same frequency
1616
* It's averaged at 30 ms on Leetcode.
1717
*/
18-
public List<Integer> topKFrequent(int[] nums, int k) {
18+
public int[] topKFrequent(int[] nums, int k) {
1919
Map<Integer, Integer> map = new HashMap();
2020
for (int i : nums) {
2121
map.put(i, map.getOrDefault(i, 0) + 1);
@@ -37,16 +37,19 @@ public List<Integer> topKFrequent(int[] nums, int k) {
3737
}
3838
}
3939
}
40-
41-
return result;
40+
int[] arr = new int[result.size()];
41+
for (int i = 0; i < arr.length; i++) {
42+
arr[i] = result.get(i);
43+
}
44+
return arr;
4245
}
4346
}
4447

4548
public static class Solution2 {
4649
/**
4750
* Use hashtable and heap, it's averaged at 100 ms on Leetocde.
4851
*/
49-
public List<Integer> topKFrequent(int[] nums, int k) {
52+
public int[] topKFrequent(int[] nums, int k) {
5053
// construct the frequency map first, and then iterate through the map
5154
// and put them into the heap, this is O(n)
5255
Map<Integer, Integer> map = new HashMap();
@@ -60,11 +63,15 @@ public List<Integer> topKFrequent(int[] nums, int k) {
6063
heap.offer(entry);
6164
}
6265

63-
List<Integer> res = new ArrayList();
66+
List<Integer> result = new ArrayList();
6467
while (k-- > 0) {
65-
res.add(heap.poll().getKey());
68+
result.add(heap.poll().getKey());
69+
}
70+
int[] arr = new int[result.size()];
71+
for (int i = 0; i < arr.length; i++) {
72+
arr[i] = result.get(i);
6673
}
67-
return res;
74+
return arr;
6875
}
6976
}
7077
}

src/test/java/com/fishercoder/_347Test.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
import org.junit.BeforeClass;
55
import org.junit.Test;
66

7-
import java.util.ArrayList;
8-
import java.util.Arrays;
9-
import java.util.List;
10-
11-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertArrayEquals;
128

139
public class _347Test {
1410
private static _347.Solution1 solution1;
1511
private static _347.Solution2 solution2;
1612
private static int[] nums;
17-
private static List<Integer> expected;
13+
private static int[] expected;
1814

1915
@BeforeClass
2016
public static void setup() {
@@ -25,26 +21,26 @@ public static void setup() {
2521
@Test
2622
public void test1() {
2723
nums = new int[]{3, 0, 1, 0};
28-
expected = new ArrayList<>(Arrays.asList(0, 3));
24+
expected = new int[]{0, 3};
2925
/**Comment out until Leetcode addresses this test case:
3026
* https://discuss.leetcode.com/topic/44237/java-o-n-solution-bucket-sort/75
3127
* Then I'll update this Solution1 code accordingly.
3228
*
3329
* My post is still un-addressed. - 3/12/2018*/
34-
//assertEquals(expected, solution1.topKFrequent(nums, 2));
30+
//assertArrayEquals(expected, solution1.topKFrequent(nums, 2));
3531
}
3632

3733
@Test
3834
public void test2() {
3935
nums = new int[]{3, 0, 1, 0};
40-
expected = new ArrayList<>(Arrays.asList(0, 3));
41-
assertEquals(expected, solution2.topKFrequent(nums, 2));
36+
expected = new int[]{0, 3};
37+
assertArrayEquals(expected, solution2.topKFrequent(nums, 2));
4238
}
4339

4440
@Test
4541
public void test3() {
46-
nums = new int[] {1, 1, 1, 2, 2, 3};
47-
expected = new ArrayList<>(Arrays.asList(1, 2));
48-
assertEquals(expected, solution1.topKFrequent(nums, 2));
42+
nums = new int[]{1, 1, 1, 2, 2, 3};
43+
expected = new int[]{1, 2};
44+
assertArrayEquals(expected, solution1.topKFrequent(nums, 2));
4945
}
5046
}

0 commit comments

Comments
 (0)