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 ...
随机推荐
- SQL中exists的使用方法
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False exists : 强调的是是否返回结果集,不要求知道返回什么, exists 与 in ...
- easyui_动态添加隐藏toolbar按钮
目标:动态添加隐藏toolbar,比如根据权限动态显示新增.修改.删除按钮等 思路:先初始化toolbar的所有按钮,加载datagrid其它信息,再根据权限显示隐藏toolbar按钮 步骤: 1.加 ...
- 【转】】CTO、技术总监、首席架构师的区别
经常有创业公司老板来拜访我,常常会拜托给我一句话:帮我找一个CTO. 我解释的多了,所以想把这个写下来,看看你到底需要的应该是啥. 一.高级程序员 如果你是一个刚刚创业的公司,公司没有专职产品经理和项 ...
- linux更改文件所有者命令chown命令的使用困惑
[berry@berry:practice] ls -lrt total -rwxrwxrwx berry berry Dec : f1.txt -rwxrwxrwx berry berry Dec ...
- 如何通俗地理解 Gradle
http://www.zhihu.com/question/30432152 一句话概括就是:依赖管理和任务执行. 像Ruby里面的bundler+rake,像iOS中的cocoapods,像node ...
- eclipse错误:Unable to read workbench state. Workbench UI layout will be reset.XML document structures
Unable to read workbench state. Workbench UI layout will be reset.XML document structures must start ...
- web图片使用
1. jpg.png.gif 适用场景 jpg 色彩丰富.大的图片例如 写实的图像,商品图片,人像,实物素材的广告banner等 png 色彩较少,有透明,或 具备较大亮度差异及强烈对比的图像,例如 ...
- mysqli 操作数据库(转)
从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...
- jetty使用教程(嵌入eclipse开发)
在eclipse下面建一个java project 建立目录结构如下: 二级目录: (备注jetty_test是工程的根目录,etc.lib.webRoot为其二级目录) 到jetty的官方网站(ht ...
- linux基本命令(2)-备份压缩命令
一.tar命令 .解压文件 .tar.gz -C /opt (解压到/opt下) / /opt/tomcat (建立链接文件) 二.zip命令 // 1.把/home目录下面的mydata目录压缩为m ...