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

 1 public class Solution {
 2     public List<String> generatePossibleNextMoves(String s) {
 3         List<String> res = new ArrayList<String>();
 4         for(int i = 1; i<s.length(); i++){
 5             if(s.charAt(i) == '+' && s.charAt(i-1) == '+'){
 6                 res.add(s.substring(0,i-1) + "--" + s.substring(i+1));
 7             }
 8         }
 9         return res;
10     }
11 }

跟上Flip Game II

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5186278.html

LeetCode 293. Flip Game相关推荐

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

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

  2. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

  3. leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)

    题目 https://leetcode.com/problems/flip-string-to-monotone-increasing/ 题解 方法1:前缀和 class Solution {publ ...

  4. LeetCode 293. 翻转游戏

    文章目录 1. 题目 2. 解题 1. 题目 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串. 你和朋友轮流将 连续 的两个 "++" 反转成 ...

  5. flip game java_LeetCode 293. Flip Game

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

  6. leetcode | 971. Flip Binary Tree To Match Preorder Traversal(DFS/preorder)

    https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 分析:题目要求,如何通过swap左右节点的方式, ...

  7. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  8. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. java 16进制与图片互转
  2. WPF入门(三)-几何图形之不规则图形(PathGeometry) (2)
  3. Linux中expect实现自动登录
  4. 008 释放技能call分析
  5. 如何让gridview中的checkbox根据数据库情况默认选中?
  6. 为什么apm代购价那么便宜_为什么长焦相机那么便宜而单反相机那么贵
  7. Qt通过ODBC读取excel文件
  8. perl语言学习笔记(3)列表与数组、子程序’、参数
  9. MegaWizard Plug-in Manager产生的目录结构及关键文件
  10. nachos操作系统初步认识
  11. 电子罗盘简单介绍和应用
  12. 可以用美图秀秀裁一寸照片
  13. 机器学习——专业名词
  14. 《C语言程序设计》第4版 何钦铭、颜晖主编 课后习题答案
  15. Enter键绑定按钮或方法
  16. PyQt5_pyqtgraph双均线组合工具
  17. 中债登——各功能快捷入口
  18. 服务端socket程序
  19. JavaScript -- event handler
  20. 了解一下winsock

热门文章

  1. 教你从0到1搭建秒杀系统-防超卖
  2. java 手动编译打包_Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...
  3. php到处excel慢,phpexcel导出数据耗时,求教
  4. python init方法是不是私有方法_为什么Python的“私有”方法实际上不是私有的?...
  5. 华为HCIA-Transmission H31-311练习题
  6. 【干货】网工面试最常见的几大问题,附答案
  7. ArgoCD + KubeVela:以开发者为中心的 GitOps
  8. 直击痛点,详解 K8s 日志采集最佳实践
  9. 云原生生态周报 Vol. 21 | Traefik 2.0 正式发布
  10. win10运行C语言的程序,win10系统运行软件提示应用程序发生异常0xc0000409的具体教程...