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 twoconsecutive "++"
into "--"
. The game ends when a person can no longer make a move and therefore the other person will be the winner.html
Write a function to compute all possible states of the string after one valid move.post
For example, given s = "++++"
, after one move, it may become one of the following states:url
[ "--++", "+--+", "++--" ]
If there is no valid move, return an empty list []
.spa
這道題讓咱們把相鄰的兩個++變成--,真不是一道難題,咱們就從第二個字母開始遍歷,每次判斷當前字母是否爲+,和以前那個字母是否爲+,若是都爲加,則將翻轉後的字符串存入結果中便可,參見代碼以下:code
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; } };
相似題目:htm
Flip Game II
blog
參考資料:ip
https://leetcode.com/problems/flip-game/description/leetcode