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 two consecutive "++" 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 [].
解析:
这道题让我们把相邻的两个++变成--,真不是一道难题,我们就从第二个字母开始遍历,每次判断当前字母是否为+,和之前那个字母是否为+,如果都为加,则将翻转后的字符串存入结果中即可
Solution:
Just iterate the string characters and check the element at i and i + 1 are '+'.
Time complexity : O(n)
Space complexity: O(n) (worst)
本文介绍了一个简单的游戏算法问题——FlipGame。玩家需将字符串中的连续两个'+'字符翻转成'--'。文章详细解析了如何通过遍历字符串并检查相邻元素的方法来实现这一过程,最终返回所有可能的状态。

367

被折叠的 条评论
为什么被折叠?



