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 ...
随机推荐
- 2.BIND服务基础及域主服务器配置
一.BIND 现今使用最晚广泛的DNS服务器软件是BIND(Berkeley Internet Name Domain),最早由伯克利大学的一名学生编写,现在最新的版本是9,由ISC(Internet ...
- (C/C++学习笔记) 五. 常变量(只读变量)和宏
五. 常变量(只读变量)和宏 ● 常变量 常变量 #include <iostream.h> //预处理文件 int main() { const d ...
- 2.15 C++常量指针this
参考: http://www.weixueyuan.net/view/6346.html 总结: 在每一个成员函数中都包含一个常量指针,我们称其为this指针,该指针指向调用本函数的对象,其值为该对象 ...
- centos6.6安装hadoop-2.5.0(三、完全分布式安装)
操作系统:centos6.6(三台服务器) 环境:selinux disabled:iptables off:java 1.8.0_131 安装包:hadoop-2.5.0.tar.gz hadoop ...
- http 请求头部解析
作者:知乎用户链接:https://www.zhihu.com/question/42696895/answer/109035792来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 多进程于多线程的区别,cpu密集型适合用什么
多线程:在单个程序中同事运行多少个线程完成不同的工作,成为线程. 线程共享内存空间,进程的内存是独立的, 同一个进程的线程间可以直接交流: 两个进程想通信,必须通过一个中间代理来实现, 一个线程可以控 ...
- nwjs 解决手指可滑动问题
package.json 中添加: "chromium-args": "--touch-events --enable-touch-drag-drop",
- 【Python】sql-内连接,左连接,右连接,union
内连接: mysql> select * from book_wangjing as book_1 inner join user_wangjing as user_1 on book_1.id ...
- 在使用MyCat和MySqL时的错误总结
在mysql中,无法连接虚拟机中的mysql. 原因:防火墙没有关闭 解决方案:service iptables stop 在mycat中,无法打开数据库的表, 原因:mycat在配置文件中设置的是自 ...
- win7英文版
下载个系统,不是一堆木马,就是一堆病毒:相对来说,我还是倾向于相信国外的镜像. https://pcriver.com/operating-systems/windows-7-ultimate-iso ...