码迷,mamicode.com
首页 > 其他好文 > 详细

914. 翻转游戏

时间:2020-03-26 01:06:15      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:find   car   oda   string   依次   nim   如何   lis   drop   

914. 翻转游戏

中文English

You are playing the following Flip Game with your friend: Given a string that contains only two characters: + and -, you can flip two consecutive "++" into "--", you can only flip one time. Please find all strings that can be obtained after one flip.

Write a program to find all possible states of the string after one valid move.

样例

Example1

Input:  s = "++++"
Output: 
[
  "--++",
  "+--+",
  "++--"
]

Example2

Input: s = "---+++-+++-+"
Output: 
[
	"---+++-+---+",
	"---+++---+-+",
	"---+---+++-+",
	"-----+-+++-+"
]
输入测试数据 (每行一个参数)如何理解测试数据?
class Solution:
    """
    @param s: the given string
    @return: all the possible states of the string after one valid move
    """
    ‘‘‘
    大致思路:
    1.初始化res = []
    2.依次循环切割两位,如果== ++的话,那么就进行转换,然后append到res里面,直到循环到最后返回res即可
    ‘‘‘
    def generatePossibleNextMoves(self,s):
        res = []
        for i in range(len(s)-1):
            if s[i:i+2] == ++:
                res.append(s[:i] + -- + s[i+2:])
        return res

 

914. 翻转游戏

标签:find   car   oda   string   依次   nim   如何   lis   drop   

原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12571823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!