8000 refactor 451 · jackx2006/Leetcode@7a249ed · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 7a249ed

Browse files
refactor 451
1 parent b90c66e commit 7a249ed

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@
5050
*/
5151
public class _451 {
5252

53-
public String frequencySort(String s) {
54-
Map<Character, Integer> map = new HashMap();
55-
for (char c : s.toCharArray()) {
56-
map.put(c, map.getOrDefault(c, 0) + 1);
57-
}
58-
List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
59-
Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue()));
60-
StringBuilder stringBuilder = new StringBuilder();
61-
for (Map.Entry<Character, Integer> entry : list) {
62-
for (int i = 0; i < entry.getValue(); i++) {
63-
stringBuilder.append(entry.getKey());
53+
public static class Solution1 {
54+
public String frequencySort(String s) {
55+
Map<Character, Integer> map = new HashMap();
56+
for (char c : s.toCharArray()) {
57+
map.put(c, map.getOrDefault(c, 0) + 1);
58+
}
59+
List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
60+
Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue()));
61+
StringBuilder stringBuilder = new StringBuilder();
62+
for (Map.Entry<Character, Integer> entry : list) {
63+
for (int i = 0; i < entry.getValue(); i++) {
64+
stringBuilder.append(entry.getKey());
65+
}
6466
}
67+
return stringBuilder.toString();
6568
}
66-
return stringBuilder.toString();
6769
}
6870
}

src/test/java/com/fishercoder/_451Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
* Created by fishercoder on 1/15/17.
1212
*/
1313
public class _451Test {
14-
private static _451 test;
14+
private static _451.Solution1 solution1;
1515
private static String expected;
1616
private static String actual;
1717
private static String input;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _451();
21+
solution1 = new _451.Solution1();
2222
}
2323

2424
@Before
@@ -31,23 +31,23 @@ public void setupForEachTest() {
3131
public void test1() {
3232
input = "tree";
3333
expected = "eert";
34-
actual = test.frequencySort(input);
34+
actual = solution1.frequencySort(input);
3535
assertEquals(expected, actual);
3636
}
3737

3838
@Test
3939
public void test2() {
4040
input = "cccaaa";
4141
expected = "aaaccc";
42-
actual = test.frequencySort(input);
42+
actual = solution1.frequencySort(input);
4343
assertEquals(expected, actual);
4444
}
4545

4646
@Test
4747
public void test3() {
4848
input = "Aabb";
4949
expected = "bbAa";
50-
actual = test.frequencySort(input);
50+
actual = solution1.frequencySort(input);
5151
assertEquals(expected, actual);
5252
}
5353
}

0 commit comments

Comments
 (0)
0