Flip Game I && II
Flip Game I
Problem Description:
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 [].
public List<String> generatePossibleNextMoves(String s) {
List<String> list = new ArrayList<String>();
if (s == null || s.length() < ) return list;
for (int i = -; (i = s.indexOf("++", i + )) >= ;) {
list.add(s.substring(, i) + "--" + s.substring(i + ));
}
return list;
}
Flip Game II
Problem Description:
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 determine if the starting player can guarantee a win.
For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".
Code is from: https://discuss.leetcode.com/topic/27287/short-java-ruby?show=64350
public boolean canWin(String s) {
for (int i=-; (i = s.indexOf("++", i+)) >= ; )
if (!canWin(s.substring(, i) + "-" + s.substring(i + ))) return true;
return false;
}
Flip Game I && II的更多相关文章
- [Locked] Flip Game I & II
Flip Game I You are playing the following Flip Game with your friend: Given a string that contains o ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- LeetCode Flip Game II
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
- 294. Flip Game II
题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...
- [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- Flip Game II -- LeetCode
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] 294. Flip Game II 翻转游戏 II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
随机推荐
- Java设计模式-装饰模式(Decorator)
顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...
- 可视化HTML编辑器
[荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛 ...
- bzoj 3172 后缀数组|AC自动机
后缀数组或者AC自动机都可以,模板题. /************************************************************** Problem: 3172 Us ...
- Sqlserver 读取EXCEL
1.1 启用本地读取设置 --启用EXEC sp_configure 'show advanced options', 1RECONFIGUREEXEC sp_configure 'Ad Hoc Di ...
- Activity启动模式 及 Intent Flags 与 栈 的关联分析
http://blog.csdn.net/vipzjyno1/article/details/25463457 Android启动模式Flags栈Task 目录(?)[+] 什么是栈 栈 ...
- thinkphp空控制器的处理
<?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...
- gcc/g++ 参数
-static 此选项将禁止使用动态库,所以,编译出来的东西,一般都很大,也不需要什么 动态连接库,就可以运行. -share 此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库.
- web后门排查与高效分析web日志技巧
今年一直大大小小的事情忙,很少有时间能静下心写个文章,所以最近博客更新也越来越少了,公司现在安全团队在我这边,一直在玩命的招人.下个月8号有一个互联网金融的会,4月在qcon北京站,都以嘉宾的身份去分 ...
- FCK编辑器漏洞总结
1.查看编辑器版本FCKeditor/_whatsnew.html————————————————————————————————————————————————————————————— 2. Ve ...
- CentOS7 最小化安装后启用无线连接网络
1.ip addr 找到自己的无线网接口 (ps:本人的是wlp2s0) 2.ip link set wlp2s0 up 打开无线网的驱动 3.ip link show wlp2s0 查看该网络接口的 ...