[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 twoconsecutive "++"
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.
For example, given s = "++++"
, after one move, it may become one of the following states:
[ "--++", "+--+", "++--" ]
If there is no valid move, return an empty list []
.
给一个只含有'+', '-'的字符串,每次可翻动两个连续的'+',求有多少种翻法。
解法:
java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Solution { public List<String> generatePossibleNextMoves(String s) { List<String> res = new ArrayList<>(); char [] arr = s.toCharArray(); for ( int i = 1 ; i < s.length(); i++) { if (arr[i] == '+' && arr[i - 1 ] == '+' ) { arr[i] = '-' ; arr[i - 1 ] = '-' ; res.add(String.valueOf(arr)); arr[i] = '+' ; arr[i - 1 ] = '+' ; } } return res; } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Solution( object ): def generatePossibleNextMoves( self , s): """ :type s: str :rtype: List[str] """ res = [] i, n = 0 , len (s) - 1 while i < n: # O(n) time if s[i] = = '+' : while i < n and s[i + 1 ] = = '+' : # O(c) time res.append(s[:i] + '--' + s[i + 2 :]) # O(n) time and space i + = 1 i + = 1 return res |
Python:
1 2 3 4 5 6 7 8 9 10 | # Time: O(c * m * n + n) = O(c * n + n), where m = 2 in this question # Space: O(n) # This solution compares O(m) = O(2) times for two consecutive "+", where m is length of the pattern class Solution2( object ): def generatePossibleNextMoves( self , s): """ :type s: str :rtype: List[str] """ return [s[:i] + "--" + s[i + 2 :] for i in xrange ( len (s) - 1 ) if s[i:i + 2 ] = = "++" ] |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // Time: O(c * n + n) = O(n * (c+1)), n is length of string, c is count of "++" // Space: O(1), no extra space excluding the result which requires at most O(n^2) space class Solution { public : vector<string> generatePossibleNextMoves(string s) { vector<string> res; int n = s.length(); for ( int i = 0; i < n - 1; ++i) { // O(n) time if (s[i] == '+' ) { for (;i < n - 1 && s[i + 1] == '+' ; ++i) { // O(c) time s[i] = s[i + 1] = '-' ; res.emplace_back(s); // O(n) to copy a string s[i] = s[i + 1] = '+' ; } } } return res; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution { public : vector<string> generatePossibleNextMoves(string s) { vector<string> res; for ( int i = 1; i < s.size(); ++i) { if (s[i] == '+' && s[i - 1] == '+' ) { res.push_back(s.substr(0, i - 1) + "--" + s.substr(i + 1)); } } return res; } }; |
类似题目:
[LeetCode] 294. Flip Game II 翻转游戏 II
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构