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 twoconsecutive "++" 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:

  1. [
  2. "--++",
  3. "+--+",
  4. "++--"
  5. ]

If there is no valid move, return an empty list [].

给一个只含有'+', '-'的字符串,每次可翻动两个连续的'+',求有多少种翻法。

解法:

java:

  1. public class Solution {
  2. public List<String> generatePossibleNextMoves(String s) {
  3. List<String> res = new ArrayList<>();
  4. char[] arr = s.toCharArray();
  5. for(int i = 1; i < s.length(); i++) {
  6. if(arr[i] == '+' && arr[i - 1] == '+') {
  7. arr[i] = '-';
  8. arr[i - 1] = '-';
  9. res.add(String.valueOf(arr));
  10. arr[i] = '+';
  11. arr[i - 1] = '+';
  12. }
  13. }
  14.  
  15. return res;
  16. }
  17. }

Python:

  1. class Solution(object):
  2. def generatePossibleNextMoves(self, s):
  3. """
  4. :type s: str
  5. :rtype: List[str]
  6. """
  7. res = []
  8. i, n = 0, len(s) - 1
  9. while i < n: # O(n) time
  10. if s[i] == '+':
  11. while i < n and s[i+1] == '+': # O(c) time
  12. res.append(s[:i] + '--' + s[i+2:]) # O(n) time and space
  13. i += 1
  14. i += 1
  15. return res

Python:

  1. # Time: O(c * m * n + n) = O(c * n + n), where m = 2 in this question
  2. # Space: O(n)
  3. # This solution compares O(m) = O(2) times for two consecutive "+", where m is length of the pattern
  4. class Solution2(object):
  5. def generatePossibleNextMoves(self, s):
  6. """
  7. :type s: str
  8. :rtype: List[str]
  9. """
  10. return [s[:i] + "--" + s[i+2:] for i in xrange(len(s) - 1) if s[i:i+2] == "++"]

C++:

  1. // Time: O(c * n + n) = O(n * (c+1)), n is length of string, c is count of "++"
  2. // Space: O(1), no extra space excluding the result which requires at most O(n^2) space
  3. class Solution {
  4. public:
  5. vector<string> generatePossibleNextMoves(string s) {
  6. vector<string> res;
  7. int n = s.length();
  8. for (int i = 0; i < n - 1; ++i) { // O(n) time
  9. if (s[i] == '+') {
  10. for (;i < n - 1 && s[i + 1] == '+'; ++i) { // O(c) time
  11. s[i] = s[i + 1] = '-';
  12. res.emplace_back(s); // O(n) to copy a string
  13. s[i] = s[i + 1] = '+';
  14. }
  15. }
  16. }
  17. return res;
  18. }
  19. };

C++:

  1. class Solution {
  2. public:
  3. vector<string> generatePossibleNextMoves(string s) {
  4. vector<string> res;
  5. for (int i = 1; i < s.size(); ++i) {
  6. if (s[i] == '+' && s[i - 1] == '+') {
  7. res.push_back(s.substr(0, i - 1) + "--" + s.substr(i + 1));
  8. }
  9. }
  10. return res;
  11. }
  12. };

  

类似题目:

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

All LeetCode Questions List 题目汇总

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

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

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

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

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

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

  5. [Swift]LeetCode293. 翻转游戏 $ Flip Game

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

  6. 【2018寒假集训 Day1】【位运算】翻转游戏

    翻转游戏(flip) [问题描述] 翻转游戏是在一个 4 格×4 格的长方形上进行的,在长方形的 16 个格上每 个格子都放着一个双面的物件.每个物件的两个面,一面是白色,另一面是黑色, 每个物件要么 ...

  7. Luogu 1764 翻转游戏 - 枚举 + 搜索

    题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...

  8. [LeetCode] 190. Reverse Bits 翻转二进制位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 294. 翻转游戏 II

    题目: 链接:https://leetcode-cn.com/problems/flip-game-ii/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...

随机推荐

  1. Android Binder机制彻底梳理一

    Binder架构图: 先来瞅一下它的整体架构图: 其中粉红部分是上层的Binder,而蓝色的则是下层的Binder,很显然上层的是依赖于下层的. 什么是Binder[有个大概了解]? 这里从几个层面来 ...

  2. Java动态代理演变之路

    1.什么是代理? 代理,英文成文Proxy.意思是你不用去做,别人代替你去处理.比如有人想找明星周董去唱歌,他需要做签约.讨论.唱歌和付款等等过程,但真正周董擅长的事情是唱歌,其他的事情可以交代给他的 ...

  3. linux中的操作记录

    在hadoop上运行jar文件:hadoop jar xxx.jar main路径 命令模式: 1.dd 删除光标所在的当前行 2.Ctrl+u 删除光标所在行光标之前的内容 3.命令模式下,按‘/’ ...

  4. 查看mysql执行时间

    mysql的 profiling不是默认打开的 查看profiling是否找开 mysql> show variables like "%pro%"; +---------- ...

  5. Java 锁(学习笔记)

    关于Java 锁的知识整理与回顾(个人笔记): 锁有哪些,分别用来干嘛? Java实现锁有两种方式,synchronized关键字和Lock (1)Lock(可判断锁状态) Lock是基于JDK层面实 ...

  6. janusgraph-创建索引出现GraphIndexStatusReport[success=false, indexName='mixedvlabel', targetStatus=[REGISTERED], notConverged={vlabel=INSTALLED}, converged={}, elapsed=PT1M0.07S]

    参考网址: https://www.cnblogs.com/Uglthinx/p/9630779.html 原因:我的是事务没有完全关闭 解决办法: 创建一个混合索引: // 在graph中有事务执行 ...

  7. 解决<c:if>无else的问题

    之前发了一个jstl的if标签博客,说是jsp没有提供<c:else>标签.于是有大佬评论,说<c:choose></c:choose>可以解决,通过查资料和敲代码 ...

  8. OKR如何解决策略执行问题

    卡普兰和诺顿在2005年发现,十分之九的组织未能执行其战略. 唐纳德·萨尔(Donald Sull)在2015年的研究中发现,有45%的中层管理人员甚至无法说出其组织的重中之重.这些中层经理基本上负责 ...

  9. js之大文件分段上传、断点续传

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  10. 洛谷 P2813【母舰】 题解

    总体思路: 输入护盾和攻击力,然后快速排序sort走起来, 排完序之后从第一个开始找,如果攻击力大于护盾,护盾继续下一个, 这个攻击力记录为0,如果小雨的话,那就攻击力继续下一个,护盾不动, 其中最为 ...