【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:]来构建

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,172评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,291评论 3 20
  • 7、贵人红土菜馆 “QQ星、老李子来了!”卫春明接电话后十分兴奋,“欢迎欢迎啊,我在村里呢,你们到...
    青春值了吗阅读 3,885评论 1 4
  • 周日下午在家里加班,睿睿一直想让我陪她出去玩,没有答应她,哭了好几次。昨天下午下班,就和爸爸说今天一定要带睿睿出去...
    英娟儿阅读 1,616评论 1 2
  • 互联网技术的发展为工作增添不少便利,但也带来了不少烦恼。 近日,浙江宁波一职工王女士因为在下班时间未及时在微信工作...
    覔三甴阅读 2,607评论 0 0

友情链接更多精彩内容