LeetCode–Flip Game
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.
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
if(s == null || s.length()){
return res;
}
for (int i=1; i<s.length(); i++){
if(s.charAt(i-1) == '+' && s.charAt(i) == '+'){
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
原题链接在这里:https://leetcode.com/problems/flip-game/ 题目: You are playing the following Flip Game with yo ...
- [LeetCode] Flip Game II 翻转游戏之二
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 ...
随机推荐
- Django 数据库连接配置(Oracle、Mysql)
一.Django Oracle连接配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracle', 'NAME': 'DEMO' ...
- vue-14-过滤
过滤器可以用在两个地方:mustache 插值和 v-bind 表达式 <!-- in mustaches --> {{ message | capitalize }} <!-- i ...
- error: illegal character '\ufeff' 的解决方案
今天使用scalac 命令编译scala文件的时候,出错了,出现如下错误提示:
- MFC Release版本串口连不上的问题
项目开发过程中发现Release版本存在连接串口时,第一次开机后,出现连接不上的问题,但在Debug版本下正常:而且只要连接上一次,Release版本就能正常连接: 解决方案: 在串口配置过程中更改为 ...
- SQL-1 选取表中某一属性最大值的所有信息 查找最晚入职员工的所有信息
题目描述 查找最晚入职员工的所有信息CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`fi ...
- 百度地图API示例:鼠标绘制点线面 控件修改
需求 :在使用地图API时,绘制工具栏控件想自己选择哪些要,哪些不要. 可以查看相应的类:官网地址: http://api.map.baidu.com/library/DrawingManager/1 ...
- Code First 迁移更新数据库
在使用 Code First 方式进行MVC程序设计中,更新数据库操作记录: 1.修改需要更新的Model,将应用程序重新编译 2.选择工具>库程序包管理器>程序包管理控制台,打开控制台, ...
- final文案+美工
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476] 文案+美工: 剧情设计+题目设计+美工: 第21关: 剧情: 计算机学 ...
- L304 What Is Death?
How should we define the death of a person? Philosophers and physicians have long pondered this ques ...
- L260
Innovative UK technology that can deliver drugs deep into the brain to treat neurological diseases, ...