原题链接在这里: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. Python之 Django 初级

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  2. git操作整理

    昨天手残 然后在GitHub for windows 上点了revert 然后就给重置了 更手残的是又给同步了 .  但是 GitHub 会保留之前的版本 . 只要删掉本次修改就可. 解决方案:  g ...

  3. mvn库

    http://maven.aliyun.com/nexus/content/groups/public/ http://mvnrepository.com/http://search.maven.or ...

  4. SQL server 2008 T-sql 总结

    数据库的实现 1.添加数据:insert [into] 表名 (字段1,字段2,···) values (值1,值2,····)     其中,into可选. 2.修改数据:update 表名 set ...

  5. 并发Socket程序设计

    1. 非阻塞并发模型 直接将socket设置为非阻塞, 轮询处理连接和接收. 缺点: 极大消耗CPU资源,不适合实际应用. 2. 信号驱动模型 当Socket文件描述符准备就绪后 内核会给进程发送一个 ...

  6. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  7. 语音01_TTS

    1.http://blog.csdn.net/u010176014/article/details/47428595 2.家里 Win7x64 安装“微软TTS5.1语音引擎(中文).msi”之后,搜 ...

  8. svg_鼠标手型

    1. 貌似是 属性 "cursor :pointer;",待测试. 2.

  9. pg数据库表接口和数据导出

    导出命令 pg_dump -U postgres --inserts -t human_info > D:\human_info_backup.sql testdb 命令说明 pg_dump:是 ...

  10. pdf2swf+flexpaper解决pdf在线阅读(类百度文库)

    1:工具准备swftools.exe 下载 http://www.swftools.org/download.html 安装至D盘 SWFTools提供了一系列将各种文件转成swf的工具: font2 ...