LeetCode刷题实战293:翻转游戏
+
and -
, you and your friend take turns to flip twoconsecutive "++"
into "--"
. The game ends when a person can no longer make a move and therefore the other person will be the winner.示例
示例:
输入: s = "++++"
输出:
[
"--++",
"+--+",
"++--"
]
注意:如果不存在可能的有效操作,请返回一个空列表 []。
解题
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == '+' && s[i - 1] == '+') {
res.push_back(s.substr(0, i - 1) + "--" + s.substr(i + 1));
}
}
return res;
}
};
评论