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 ...
随机推荐
- 斯坦福大学 iOS 开发公开课总结
斯坦福大学 iOS 开发公开课总结 前言 iPhone 开发相关的教程中最有名的,当数斯坦福大学发布的 "iPhone 开发公开课 " 了.此公开课在以前叫做<iPho ...
- Maya 学习资料
罗其胜3d角色强化 CGwhat-Maya变形金刚擎天柱建模教程 Pixar in the box - khan academy Siggraph历届优秀动画 CG软件发展史:MAYA动画十年历程 m ...
- Redhat系列Linux的基础命令
为网卡配置静态IP地址vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0ONBOOT=yesBOOTPROTO=staticHWADDR=0 ...
- 分布式架构高可用架构篇_07_MySQL主从复制的配置(CentOS-6.7+MySQL-5.6)
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- NBOJv2 Problem 1009 蛤玮的魔法(二分)
Problem 1009: 蛤玮的魔法 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %ll ...
- Ubuntu 12.04 LTS 及ubuntu14.10 -- NFS安装
在Linux 服务器上配置好NFS 根文件系统后,在单板侧挂载NFS 文件系统,具体操作如下:ifconfig eth0 hw ether 00:10:85:18:01:84 /*配置MAC地址*/i ...
- FastDFS安装、配置、部署
FastDFS是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,FastDFS非常适用于基于文件服务的站点,例如图片分享和视频分享网站. Fas ...
- Nginx 笔记与总结(1)编译安装
Nginx 可以承受 3 万并发连接数,Apache 默认最大连接数是 256 个. 编译安装 ① 下载 在 Nginx 的主页 http://nginx.org/ 下载最新的 stable vers ...
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [1] 单例模式连接数据库
单例模式 单例模式三大原则: ① 构造函数需要标记为非 public (防止外部使用 new 操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化: ② 拥有一个保存类的实例的静态成员变量 ...
- 20145317彭垚 《Java程序设计》第五次实验报告
20145317彭垚实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21: ...