914. Flip Game
class Solution {
public:
/**
* @param s: the given string
* @return: all the possible states of the string after one valid move
*/
vector<string> generatePossibleNextMoves(string &s) {
// write your code here
//寻找所有的‘++’然后变‘--’
vector<string> res;
if(s.size() == 0){
return res;
}
for(int i = 0; i < s.size() - 1; i++){
if(s[i] == '+' && s[i + 1] == '+'){
s[i]= '-';
s[i + 1] = '-';
res.push_back(s);
s[i] = '+';
s[i + 1] = '+';
}
}
return res;
}
};
2019年3月7日16:29:43二刷
class Solution { public: /** * @param s: the given string * @return: all the possible states of the string after one valid move */ vector<string> generatePossibleNextMoves(string &s) { // write your code here // all possible //简单的回溯法的运用,还是有参考意义的 vector<string> res; if(s.size() < 2){ return res; } for(int i = 0; i < s.size() - 1; i++){ if(s[i] == '+' && s[i + 1] == '+'){ s[i] = '-'; s[i + 1] = '-'; res.push_back(s); s[i] = '+'; s[i + 1] = '+'; } } return res; } };
public:
/**
* @param s: the given string
* @return: all the possible states of the string after one valid move
*/
vector<string> generatePossibleNextMoves(string &s) {
// write your code here
//寻找所有的‘++’然后变‘--’
vector<string> res;
if(s.size() == 0){
return res;
}
for(int i = 0; i < s.size() - 1; i++){
if(s[i] == '+' && s[i + 1] == '+'){
s[i]= '-';
s[i + 1] = '-';
res.push_back(s);
s[i] = '+';
s[i + 1] = '+';
}
}
return res;
}
};
2019年3月7日16:29:43二刷
class Solution { public: /** * @param s: the given string * @return: all the possible states of the string after one valid move */ vector<string> generatePossibleNextMoves(string &s) { // write your code here // all possible //简单的回溯法的运用,还是有参考意义的 vector<string> res; if(s.size() < 2){ return res; } for(int i = 0; i < s.size() - 1; i++){ if(s[i] == '+' && s[i + 1] == '+'){ s[i] = '-'; s[i + 1] = '-'; res.push_back(s); s[i] = '+'; s[i + 1] = '+'; } } return res; } };
Comments
Post a Comment