Skip to content

Commit 147c4a5

Browse files
[N-0] refactor 17
1 parent be637da commit 147c4a5

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,33 @@
1818

1919
public class _17 {
2020

21-
public List<String> letterCombinations(String digits) {
22-
List<String> result = new ArrayList();
23-
if (digits.length() == 0) {
24-
return result;
25-
}
21+
public static class Solution1 {
22+
public List<String> letterCombinations(String digits) {
23+
List<String> result = new ArrayList();
24+
if (digits.length() == 0) {
25+
return result;
26+
}
2627

27-
String[] digits2Letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
28+
String[] digits2Letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
2829

29-
result.add("");//this line is important, otherwise result is empty and Java will default it to an empty String
30-
for (int i = 0; i < digits.length(); i++) {
31-
result = combine(digits2Letters[digits.charAt(i) - '0'], result);
32-
}
30+
result.add("");//this line is important, otherwise result is empty and Java will default it to an empty String
31+
for (int i = 0; i < digits.length(); i++) {
32+
result = combine(digits2Letters[digits.charAt(i) - '0'], result);
33+
}
3334

34-
return result;
35-
}
35+
return result;
36+
}
3637

37-
List<String> combine(String letters, List<String> result) {
38-
List<String> newResult = new ArrayList();
38+
List<String> combine(String letters, List<String> result) {
39+
List<String> newResult = new ArrayList();
3940

40-
for (int i = 0; i < letters.length(); i++) {
41-
//the order of the two for loops doesn't matter, you could swap them and it still works.
42-
for (String str : result) {
43-
newResult.add(str + letters.charAt(i));
41+
for (int i = 0; i < letters.length(); i++) {
42+
//the order of the two for loops doesn't matter, you could swap them and it still works.
43+
for (String str : result) {
44+
newResult.add(str + letters.charAt(i));
45+
}
4446
}
47+
return newResult;
4548
}
46-
return newResult;
4749
}
4850
}

src/test/java/com/fishercoder/_17Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212
import static org.junit.Assert.assertTrue;
1313

1414
public class _17Test {
15-
private static _17 test;
15+
private static _17.Solution1 solution1;
1616
private static String digits;
1717
private static List<String> expected;
1818
private static List<String> actual;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _17();
22+
solution1 = new _17.Solution1();
2323
}
2424

2525
@Test
2626
public void test1() {
2727
digits = "2";
28-
actual = test.letterCombinations(digits);
28+
actual = solution1.letterCombinations(digits);
2929
expected = new ArrayList<>(Arrays.asList("a", "b", "c"));
3030
assertEquals(expected, actual);
3131
}
3232

3333
@Test
3434
public void test2() {
3535
digits = "23";
36-
actual = test.letterCombinations(digits);
36+
actual = solution1.letterCombinations(digits);
3737
expected = new ArrayList<>(Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"));
3838
/**order doesn't matter, so we check like below*/
3939
assertTrue(expected.containsAll(actual) && actual.containsAll(expected));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy