题目:
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive “++” into “—“. The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to compute all possible states of the string after one valid move.
For example, given s = “++++”, after one move, it may become one of the following states:
[
“—++”,
“+—+”,
“++—“
]
If there is no valid move, return an empty list []
.
翻译文:
你正在和你的朋友玩下面的翻转游戏:给定一个字符串,它只包含这两个字符:+和-,你和你的朋友轮流把两个连续的“++”翻转成“——”。游戏结束时,一个人不能再采取行动,因此另一个人将是赢家。
写一个函数来计算一个有效移动后字符串的所有可能状态。
``
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
// 哈哈哈, 没看懂
List<Character> list = new ArrayList<>();
Char []str = s.toCharArray();
for(int i = 1; i < s.length(); i++){
if(str[i] == '+' && str[i - 1] == '+'){
str[i] = '-';
str[i - 1] = '-';
list.add(String.valueOF(str));
str[i] = '+';
str[i - 1] = '+';
}
}
return list;
}
}
``