-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode294.java
51 lines (46 loc) · 1.29 KB
/
LeetCode294.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) ysx. 2020-2020. All rights reserved.
*/
package com.ysx.leetcode.easy;
import java.util.ArrayList;
import java.util.List;
/**
* @author youngbear
* @email youngbear@aliyun.com
* @date 2020/1/8 21:04
* @blog https://blog.csdn.net/next_second
* @github https://github.com/YoungBear
* @description 294. 翻转游戏 II
* https://leetcode-cn.com/problems/flip-game-ii/
*/
public class LeetCode294 {
/**
* 递归法
*
* @param s
* @return
*/
public boolean canWin(String s) {
// 获得第一次操作后所有的情况
List<String> stringList = generatePossibleNextMoves(s);
for (String string : stringList) {
// 如果下一个失败,则该次成功
if (!canWin(string)) {
return true;
}
}
return false;
}
private List<String> generatePossibleNextMoves(String s) {
List<String> resultList = new ArrayList<>();
if (null == s || s.length() == 0) {
return resultList;
}
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
resultList.add(s.substring(0, i) + "--" + s.substring(i + 2));
}
}
return resultList;
}
}