【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 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.

class Solution(object):

    def generatePossibleNextMoves(self, s):

        """

        :type s: str

        :rtype: List[str]

        """

        if not s: return []

        res = []

        for i in range(1,len(s)):

            if s[i-1:i+1]=='++':

                s1 = s[:i-1]+'--'+s[i+1:]

                res.append(s1)

        return res

1 注意res.append(s1)要和s1 = s[:i-1]+'--'+s[i+1:]在同一个缩进上,不然就会出现没有定义就调用的错误

2 比如当没有‘++’满足题意,那就直接走res.append(s1)这句,就会出现没有定义就调用的错误

3 不能写成s[i-1:i+1]='--'这种赋值形式,没有这样的赋值,只能通过s1 = s[:i-1]+'--'+s[i+1:]来构建

你可能感兴趣的:(【Leetcode】293. Flip Game)