Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c3a590b

Browse files
refactor 247
1 parent 8b6ec46 commit c3a590b

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

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

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,34 @@
44
import java.util.Arrays;
55
import java.util.List;
66

7-
/**
8-
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
9-
10-
Find all strobogrammatic numbers that are of length = n.
11-
12-
For example,
13-
Given n = 2, return ["11","69","88","96"].
14-
15-
Hint:
16-
17-
Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.
18-
*/
197
public class _247 {
20-
public List<String> findStrobogrammatic(int n) {
21-
return recursiveHelper(n, n);
22-
}
23-
24-
private List<String> recursiveHelper(int n, int m) {
25-
if (n == 0) {
26-
return new ArrayList<>(Arrays.asList(""));
27-
}
28-
if (n == 1) {
29-
return new ArrayList<>(Arrays.asList("0", "1", "8"));
8+
public static class Solution1 {
9+
public List<String> findStrobogrammatic(int n) {
10+
return recursiveHelper(n, n);
3011
}
3112

32-
List<String> list = recursiveHelper(n - 2, m);
33-
List<String> res = new ArrayList<String>();
13+
private List<String> recursiveHelper(int n, int m) {
14+
if (n == 0) {
15+
return new ArrayList<>(Arrays.asList(""));
16+
}
17+
if (n == 1) {
18+
return new ArrayList<>(Arrays.asList("0", "1", "8"));
19+
}
3420

35-
for (int i = 0; i < list.size(); i++) {
36-
String s = list.get(i);
37-
if (n != m) {
38-
res.add("0" + s + "0");
21+
List<String> list = recursiveHelper(n - 2, m);
22+
List<String> res = new ArrayList<>();
23+
24+
for (int i = 0; i < list.size(); i++) {
25+
String s = list.get(i);
26+
if (n != m) {
27+
res.add("0" + s + "0");
28+
}
29+
res.add("1" + s + "1");
30+
res.add("6" + s + "9");
31+
res.add("8" + s + "8");
32+
res.add("9" + s + "6");
3933
}
40-
res.add("1" + s + "1");
41-
res.add("6" + s + "9");
42-
res.add("8" + s + "8");
43-
res.add("9" + s + "6");
34+
return res;
4435
}
45-
return res;
4636
}
4737
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._247;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _247Test {
12+
private static _247.Solution1 solution1;
13+
private static List<String> expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _247.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
expected = List.of("11","69","88","96");
23+
assertEquals(expected, solution1.findStrobogrammatic(2));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)