原题链接在这里: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 Flip Game的更多相关文章

  1. [LeetCode] Flip Game 翻转游戏之二

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

  2. [LeetCode] Flip Game 翻转游戏

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

  3. LeetCode Flip Game II

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

  4. [LeetCode] Flip Game II 翻转游戏之二

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

  5. LeetCode–Flip Game

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

  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 293. Flip Game

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

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

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

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

随机推荐

  1. FFmpeg - 音频解码过程

    1. 注册所有解码器 av_register_all(); 2. Codec & CodecContext AVCodec* codec = avcodec_find_decoder(CODE ...

  2. Git 的使用感受

    Git 的使用感受 从开始工作到现在,在公司里面一直用 svn 来做版本管理.大约半年前听说了 Git,因为 Git 的光辉相当耀眼,作者是 Linus Torvalds,被大量的开源软件采用,如 j ...

  3. js - 犀牛学习

    1.id选择器 var ul = document.getElementById("empty"); 2.属性和事件配置 li.className = "ditem&qu ...

  4. C# - Dbhelp

    dbhelp.cs using System; using System.Data; using System.Data.Common; using System.Configuration; pub ...

  5. GDB打印STL容器内容

    GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...

  6. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  7. maven相关概念

    1.maven仓库分为:本地仓库,远程仓库.远程仓库分为私服.中央仓储和其他公共库. 2.mvn clean install 部署到本地仓库 3.mvn clean deploy 部署到远程仓储 4. ...

  8. Memcached 笔记与总结(6)PHP 实现 Memcached 的一致性哈希分布算法

    首先创建一个接口,有 3 个方法: addServer:添加一个服务器到服务器列表中 removeServer:从服务器列表中移除一个服务器 lookup:在当前的服务器列表中找到合适的服务器存放数据 ...

  9. mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干

    1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...

  10. python 执行文件时传参

    ## test.py ## ####################### import sys if __name__ == "__main__": args = sys.arg ...