示例
示例:输入: 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;
}
};
好了,今天的文章就到这里。