LeetCode–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. 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 [].
若是连着的"++", 就把这段用"--"替代放到res中.
Note: 当i == s.length()-1走到最后一位时. s.substring(i+1), 不会out of index, 会返回"". 但再大就不行了.
Time Complexity: O(n). Space: O(1) regardless res.
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
if(s == null || s.length()){
return res;
}
for (int i=1; i<s.length(); i++){
if(s.charAt(i-1) == '+' && s.charAt(i) == '+'){
res.add(s.substring(0,i-1) + "--" + s.substring(i+1));
}
}
return res;
}
}
LeetCode–Flip Game的更多相关文章
- [LeetCode] Flip Game 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode Flip Game II
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
- LeetCode Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/ 题目: You are playing the following Flip Game with yo ...
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- LeetCode 293. Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...
- [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- LeetCode 971. Flip Binary Tree To Match Preorder Traversal
原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...
随机推荐
- export及export default
const a=2; const b=3; const c=function(){console.log(a+b} export a; export b; export default c; 如上文件 ...
- sql语句最后一行显示统计。
SELECT id, username, id_Num FROM users UNION ALL SELECT '合计', count(*), null FROM users ORDER BY id_ ...
- 如何查看.java文件的字节码(原码)
出自于:https://www.cnblogs.com/tomasman/p/6751751.html 直接了解foreach底层有些困难,我们需要从更简单的例子着手.下面上一个简单例子: 1 pub ...
- VSTO:使用C#开发Excel、Word【5】
<Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath >——By Eric C ...
- 深入理解java虚拟机----java技术体系(一)
1.java技术体系 举例: class文件格式:如下图所示,java源代码可以根据不同的编译器可以编译成不同的代码.即可以自定义语言规范比如beanshell,并编写代码; 然后自己编写java编译 ...
- 2.13 C++拷贝构造函数
参考:http://www.weixueyuan.net/view/6344.html 总结: 如果拷贝构造函数的参数不是对象的引用,则是不允许的.如 book(book b); 是无法编译通过的. ...
- VCL界面控件DevExpress VCL发布v18.2.2|附下载
DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...
- Ubuntu16.04 安装Tensorflow-CPU
最近我开始学习深度学习框架Tensorflow,一开始在windows平台下的anaconda下安装,由于anaconda安装几次后navigator打开老是出现闪退的问题,所以决定换个ubuntu下 ...
- Oracle远程登录命令
sqlplus登陆方式 sqlplus有几种登陆方式 比如: 1.C: > sqlplus "/as sysdba" C: > sqlplus / as sysdba ...
- AndroidSDK 自带定位工具 uiautomatorviewer
前言:uiautomatorviewer是androidSDK自带的定位工具 1.打开目录D:\Android\androidSDK\tools\bin 2.点击启动uiautomator,页面显示如 ...