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 ...
随机推荐
- lvs原理和实战
lvs-dr原理 lvs-nat原理: 当我们的网站流量越来越大时一台web服务器已经无法满足需求了,我们该如何解决呢??把服务器连接起来实现负载均衡或许是个不错的办法..下面我就来看看怎么实现吧 ...
- Hbuild - 使用海马玩模拟器调试
http://ask.dcloud.net.cn/question/13605 通过海马玩CMD(Droid4X_Settings_Tools_v1.1.5.bat) 设置模拟器的端口 269 ...
- POI设置边框
在做一个电子表格时,边框的设置有时是必不可少的.这一节就来介绍边框,设置时,可以指定边框的位置,边框的种类,边框的顔色. 首先是边框的位置和种类.对单元格设置边框时,有上下左右位置之分,所以POI也准 ...
- 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3
一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...
- 做bbs论坛项目的收获(1)
1. 当两张表出现关联的时候,关联的外键,千万不能是汉字,千万不能使汉字,因为这个问题,直接导致项目进行不下去..... 顺便记录一下知识点(一) 当用frameset设置网页的结构的时候 eg: & ...
- PDB文件:每个开发人员都必须知道的
PDB Files: What Every Developer Must Knowhttp://www.wintellect.com/CS/blogs/jrobbins/archive/2009/05 ...
- servlet等一些砸碎的
1:servlet 中的synchronized 关键字能保证一次只有一个线程 2:servlet的线程问题只有在大量的方位时 3:AutoCloseable接口:资源自动关闭 4:EntityUti ...
- 启用“关闭自动根证书更新”,解决Windows系统各种卡顿的问题(Visual studio 卡、远程桌面mstsc卡、SVN卡)
最近,发现在Win7下面一系列操作都会出现卡顿的情况: 1. Visual studio 启动调试和关闭调试时,都会卡上半分钟左右 2. 使用远程桌面mstsc.exe,点击连接时,也会卡上半分钟 ...
- c语言学习感想
接触c语言已经2个多月了,在这段期间按时的完成了作业,上课能够较好的听讲,因此我获得了老师奖励的小黄衫. 同时,希望自己能够学好c语言! 学习感受与心得 因为兴趣,选择了计算机这专业,我从遥远的南方来 ...
- BLE链路层状态机
BLE的Link层,应当是了解BLE需要首先熟悉的一部分,BLE的Controller部分主要都在围绕这一部分实现的.Link层的内容规定了BLE底层是怎么实现蓝牙设备之间的控制,数据传输等等的.Li ...