首页 > 其他 > 详细

914. 翻转游戏

时间:2020-03-26 10:07:54      阅读:50      评论:0      收藏:0      [点我收藏+]

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. 翻转游戏

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!