原题链接在这里: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;
}
}

跟上Flip Game II

LeetCode 293. Flip Game的更多相关文章

  1. [LeetCode] 293. Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  2. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  3. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. 【LeetCode】293. Flip Game 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  5. 293. Flip Game

    题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...

  6. #Leetcode# 951. Flip Equivalent Binary Trees

    https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...

  7. [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), ...

  8. LeetCode 971. Flip Binary Tree To Match Preorder Traversal

    原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...

  9. LeetCode 294. Flip Game II

    原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...

随机推荐

  1. CodeForces - 691E Xor-sequences 【矩阵快速幂】

    题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...

  2. VC6.0 开发 64 位程序

    1. 设置平台SDK(如:Microsoft platform sdk 2003),选择64位的编译.链接环境. setenv /XP64 /DEBUG 2. 利用这个环境启动VC6.0. msdev ...

  3. 014_HDFS存储架构、架构可靠性分析、副本放置策略、各组件之间的关系

    1.HDFS存储架构

  4. WEB-INF有关的目录路径总结、转向方式: forward 重定向方式: Redirect

    WEB-INF有关的目录路径总结 1.资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了. 2.页面放在WEB-INF目录下面,这样可以限制访问,提 ...

  5. PHP用*隐藏中文问题

    中文截取 function substr_cut($user_name){ $strlen= mb_strlen($user_name, 'utf-8'); $firstStr= mb_substr( ...

  6. centos7下安装tomcat7

    1 安装说明安装环境:CentOS-7.0.1611安装方式:源码安装软件:apache-tomcat-7.0.75.tar.gz 下载地址:http://tomcat.apache.org/down ...

  7. XXL-Job高可用集群搭建

    如果XXL-Job admin挂掉就完蛋了,所有任务无法执行 调度中心:管理任务的触发 调度中心如何实现集群? XXL-Job如何实现集群? 底层已经实现好了!文档里面有的 如果想实现Job集群:   ...

  8. json01-json简介和语法

    JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法.类似 XML,但比 XML 更小.更快,更易解析,是轻量级的 ...

  9. 【转载】JAVA多线程读取、操作List集合

    本文转载自:http://blog.csdn.net/wang1989cs/article/details/47663565 import java.util.ArrayList; import ja ...

  10. static 关键字详解 static方法调用非static属性和方法

    静态的属性和方法在内存中的存放地址与非静态的是不同的,静态的是存放在static区,它意味着静态方法是没有this的,所以我们不可以从一个static方法内部发出对非static方法的调用.但是反之是 ...