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 ...
随机推荐
- git for windows 入门随笔
引言: Git 是当前最流行的集中化的版本控制程序之一(版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统),Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件 ...
- Freemarker-标签使用
FreeMarker标签使用 FreeMarker模板文件主要有4个部分组成 1.文本,直接输出的部分 2.注释,即<#--...-->格式不会输出 3.插值(Interpolatio ...
- ES6的模块、构建工具及应用的发布
作者:寸志链接:https://zhuanlan.zhihu.com/p/19569085来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 总的说来就是按照将来的标准书写 ...
- 在网络7层协议中,如果想使用UDP协议达到TCP协议的效果,可以在哪层做文章?(QQ 为什么采用 UDP 协议,而不采用 TCP 协议实现?)
为了解决这题,可以具体看看下面这个讨论. 解灵运工程师 185 人赞同 某次架构师大会上那个58同城做即时通信的人说:原因是因为当时没有epoll这种可以支持成千上万tcp并发连接的技术,所以他们使用 ...
- UVA 1398 Meteor
传送门 Solution: 记一颗流星在视野内的时间段为(L, R), 为了使所有(L, R)都取整数,首先将坐标放大. 放大倍数可取为 LCM(1, 2, ..., 10)= 2520 接着计算:从 ...
- p2p软件如何穿透内网进行通信
http://blog.chinaunix.net/uid-22326462-id-1775108.html 首先先介绍一些基本概念: NAT(Network Address Translators) ...
- Android学习笔记01-Mac下搭建Java开发环境
一 安装JDK 下载 mac 下专用的jdk1.7, 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downlo ...
- electron加载jquery报错问题
<!-- Insert this line above script imports --> <script>if (typeof module === 'object') { ...
- dede5.7前台插入恶意JS代码
这个问题应该很久了 最近发现有用这个的蠕虫,dede 前台提交友情链接 只用htmlspecialchars简单处理了一下 可以插入代码plus/flink_add.php 提交: 表单中提交 图片地 ...
- Jquery 表单操作
文本框,文本区域: 获取值: 1.$("#txt").attr("value"); 2. $("txt").val(); 单选按钮: 获取值 ...