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
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12571823.html
时间: 2024-10-10 02:27:09