LeetCode Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/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 []
.
题解:
若是连着的"++", 就把这段用"--"替代放到res中.
Note: 当i == s.length()-1走到最后一位时. s.substring(i+1), 不会out of index, 会返回"". 但再大就不行了.
Time Complexity: O(n). Space: O(1) regardless res.
AC Java:
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<String>();
for(int i = 1; i<s.length(); i++){
if(s.charAt(i) == '+' && s.charAt(i-1) == '+'){
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 II 翻转游戏之二
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# 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 ...
随机推荐
- JAVA_RSA密钥生成
在网上找了下RSA的密钥的创建,结果全是用java序列化PublicKey和PrivateKey来保存,就自己写了个RSA公钥和私钥的创建,及进行Base64编码后保存. 这里用到了 bcprov-j ...
- Repeater控件中的三目运算
<asp:Repeater ID="rptimg" runat="server"> <ItemTemplate> ...
- NSString、NSMutableString基本用法
NSString其实是一个对象类型.NSString是NSObject(Cocoa Foundation的基础对象)的子类 一.NSString的创建 1.创建常量字符串.NSString *astr ...
- CSS3时钟式进度条
CSS3时钟式进度条,加载完成时生成一个圆,数字慢慢变成100,适时的显示加载进度.友情提醒,如果预览时网页左下角提示错误,刷新一下就可以看到效果了:实际使用中不会出现这样的问题. <!DOCT ...
- 上传文件及$_FILES的用法实例
Session变量($_SESSION):�php的SESSION函数产生的数据,都以超全局变量的方式,存放在$_SESSION变量中.1.Session简介SESSION也称为会话期,其是存储在服务 ...
- [APAC]查找资产表
$sn = Read-Host -Prompt "请输入SN号(7位 or 10位)" $xl = New-Object -ComObject "Excel.Applic ...
- 使用Eclipse自带的Axis1插件生成WSDL文件
首先创建一个web工程,创建过程如下: 如果选择Apache Tomcat v5.5,Dynamic web module version最高只能选择2.4,填写完成后点击“下一步”: 填写默认输出文 ...
- [办公自动化]利用Acrobat完成问卷调查或者考试卷
整体思路:(软件环境Acrobat) 1.制作问卷. 采用word制作,制作基础页面,然后倒入.自己亲测时,发现一般的文字域是可以的,但是单选按钮就不能导入. 如果是考试卷,可以利用word制作基础页 ...
- GDC2016 【巫师3 狂猎】的游戏事件工作流
巫师3 狂猎(The Witcher 3: Wild Hunt )的游戏事件工作流 http://game.watch.impress.co.jp/docs/news/20160320_74916 ...
- ecshop 完美解决动态ip登录超时和购物车清空问题
ecshop 完美解决动态ip登录超时和购物车清空问题 ECSHOP模板/ecshop开发中心(www.68ecshop.com) / 2014-05-06 前一段时间,ECSHOP开发中心的一个客户 ...