Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3a590b

Browse files
refactor 247
1 parent8b6ec46 commitc3a590b

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
importjava.util.Arrays;
55
importjava.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
publicclass_247 {
20-
publicList<String>findStrobogrammatic(intn) {
21-
returnrecursiveHelper(n,n);
22-
}
23-
24-
privateList<String>recursiveHelper(intn,intm) {
25-
if (n ==0) {
26-
returnnewArrayList<>(Arrays.asList(""));
27-
}
28-
if (n ==1) {
29-
returnnewArrayList<>(Arrays.asList("0","1","8"));
8+
publicstaticclassSolution1 {
9+
publicList<String>findStrobogrammatic(intn) {
10+
returnrecursiveHelper(n,n);
3011
}
3112

32-
List<String>list =recursiveHelper(n -2,m);
33-
List<String>res =newArrayList<String>();
13+
privateList<String>recursiveHelper(intn,intm) {
14+
if (n ==0) {
15+
returnnewArrayList<>(Arrays.asList(""));
16+
}
17+
if (n ==1) {
18+
returnnewArrayList<>(Arrays.asList("0","1","8"));
19+
}
3420

35-
for (inti =0;i <list.size();i++) {
36-
Strings =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 =newArrayList<>();
23+
24+
for (inti =0;i <list.size();i++) {
25+
Strings =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+
returnres;
4435
}
45-
returnres;
4636
}
4737
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._247;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importjava.util.List;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_247Test {
12+
privatestatic_247.Solution1solution1;
13+
privatestaticList<String>expected;
14+
15+
@BeforeClass
16+
publicstaticvoidsetup() {
17+
solution1 =new_247.Solution1();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
expected =List.of("11","69","88","96");
23+
assertEquals(expected,solution1.findStrobogrammatic(2));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp