?LeetCode刷題實(shí)戰(zhàn)293:翻轉(zhuǎn)游戲
+ 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 = "++++"
輸出:
[
"--++",
"+--+",
"++--"
]
注意:如果不存在可能的有效操作,請(qǐng)返回一個(gè)空列表 []。
解題
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;
}
};
評(píng)論
圖片
表情
