Skip to content

Commit be573fe

Browse files
refactor 294
1 parent 3014c12 commit be573fe

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@
1818
*/
1919
public class _294 {
2020

21-
public boolean canWin(String s) {
22-
List<String> res = new ArrayList<String>();
23-
char[] charArray = s.toCharArray();
24-
for (int i = 0; i < s.length() - 1; i++) {
25-
if (charArray[i] == '+' && charArray[i + 1] == '+') {
26-
//change these two bits to '-'
27-
charArray[i] = '-';
28-
charArray[i + 1] = '-';
29-
res.add(String.valueOf(charArray));
30-
//change these two bits back to '+' for its next move
31-
charArray[i] = '+';
32-
charArray[i + 1] = '+';
21+
public static class Solution1 {
22+
public boolean canWin(String s) {
23+
List<String> res = new ArrayList<>();
24+
char[] charArray = s.toCharArray();
25+
for (int i = 0; i < s.length() - 1; i++) {
26+
if (charArray[i] == '+' && charArray[i + 1] == '+') {
27+
//change these two bits to '-'
28+
charArray[i] = '-';
29+
charArray[i + 1] = '-';
30+
res.add(String.valueOf(charArray));
31+
//change these two bits back to '+' for its next move
32+
charArray[i] = '+';
33+
charArray[i + 1] = '+';
34+
}
3335
}
34-
}
35-
/**The above part is the same of Flip Game I.
36-
* The only added part is the following piece of logic (so-called backtracking.)*/
37-
for (String str : res) {
38-
if (!canWin(str)) {
39-
return true;
36+
/**The above part is the same of Flip Game I.
37+
* The only added part is the following piece of logic (so-called backtracking.)*/
38+
for (String str : res) {
39+
if (!canWin(str)) {
40+
return true;
41+
}
4042
}
43+
return false;
4144
}
42-
return false;
4345
}
4446

4547
}

src/test/java/com/fishercoder/_294Test.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
* Created by stevesun on 5/29/17.
1111
*/
1212
public class _294Test {
13-
private static _294 test;
13+
private static _294.Solution1 solution1;
1414

15-
@BeforeClass
16-
public static void setup() {
17-
test = new _294();
18-
}
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _294.Solution1();
18+
}
1919

20-
@Test
21-
public void test1() {
22-
assertEquals(true, test.canWin("++++"));
23-
}
20+
@Test
21+
public void test1() {
22+
assertEquals(true, solution1.canWin("++++"));
23+
}
2424
}

0 commit comments

Comments
 (0)