|
18 | 18 | */
|
19 | 19 | public class _294 {
|
20 | 20 |
|
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 | + } |
33 | 35 | }
|
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 | + } |
40 | 42 | }
|
| 43 | + return false; |
41 | 44 | }
|
42 |
| - return false; |
43 | 45 | }
|
44 | 46 |
|
45 | 47 | }
|
0 commit comments