[LeetCode] 293. Flip Game 翻转游戏
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.
Write a function to compute all possible states of the string after one valid move.
For example, given s = "++++"
, after one move, it may become one of the following states:
[ "--++", "+--+", "++--" ]
If there is no valid move, return an empty list []
.
这道题让我们把相邻的两个 ++ 变成 --,真不是一道难题,就从第二个字母开始遍历,每次判断当前字母是否为+,和之前那个字母是否为+,如果都为加,则将翻转后的字符串存入结果中即可,参见代码如下:
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; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/293
类似题目:
参考资料:
https://leetcode.com/problems/flip-game/description/
https://leetcode.com/problems/flip-game/discuss/73901/4-lines-in-Java
https://leetcode.com/problems/flip-game/discuss/73902/Simple-solution-in-Java
相关文章
- Leetcode: Max Points on a line
- 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
- LeetCode 算法题详解 JAVA 版 开始刷题啦~
- 132、【贪心算法】leetcode ——45. 跳跃游戏 II(贪心策略)(C++版本)
- 【LeetCode】85. Maximal Rectangle
- [LeetCode] 1025. Divisor Game 除数游戏
- [LeetCode] 837. New 21 Game 新二十一点游戏
- [LeetCode] Chalkboard XOR Game 黑板亦或游戏
- [LeetCode] Knight Probability in Chessboard 棋盘上骑士的可能性
- [LeetCode] 488. Zuma Game 祖玛游戏
- [LeetCode] 379. Design Phone Directory 设计电话目录
- [LeetCode] Employees Earning More Than Their Managers 员工挣得比经理多
- [LeetCode] 312. Burst Balloons 打气球游戏
- [LeetCode] 289. Game of Life 生命游戏
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵之二
- leetcode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(中等)