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:

[
"--++",
"+--+",
"++--"
]

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

这道题让我们把相邻的两个 ++ 变成 --,真不是一道难题,就从第二个字母开始遍历,每次判断当前字母是否为+,和之前那个字母是否为+,如果都为加,则将翻转后的字符串存入结果中即可,参见代码如下:

class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '+' && s[i - ] == '+') {
res.push_back(s.substr(, i - ) + "--" + s.substr(i + ));
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/293

类似题目:

Flip Game II

参考资料:

https://leetcode.com/problems/flip-game/description/

https://leetcode.com/problems/flip-game/discuss/73901/4-lines-in-Java

https://leetcode.com/problems/flip-game/discuss/73902/Simple-solution-in-Java

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

[LeetCode] 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] Flip Game 翻转游戏之二

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

  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. [Swift]LeetCode293. 翻转游戏 $ Flip Game

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

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

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

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

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

  7. 294. 翻转游戏 II

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

  8. NC235250 牛可乐的翻转游戏

    NC235250 牛可乐的翻转游戏 题目 题目描述 牛可乐发明了一种新型的翻转游戏! 在一个有 \(n\) 行 \(m\) 列的棋盘上,每个格子摆放有一枚棋子,每一枚棋子的颜色要么是黑色,要么是白色. ...

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

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

随机推荐

  1. 安卓Design包之TabLayout控件的简单使用

    Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...

  2. wordpress插件bug排查后记(记一次由于开启memecached引起的插件bug)

    这篇文章是写给自己的. 周三的时候我在维护公司的一个wordpress项目页面时发现了一个非常奇怪的情况:当我尝试更新网站上的一个页面后,在wordpress后台的编辑器中发现其内容并没有按我预期的将 ...

  3. Navisworks API 简单二次开发 (自定义工具条)

    在Navisworks软件运行的时候界面右侧有个工具条.比较方便.但是在二次开发的时候我不知道在Api那里调用.如果有网友知道请告诉我.谢谢. 我用就自己设置一个工具.界面比较丑!没有美工. 代码: ...

  4. Scala Macros - scalamela 1.x,inline-meta annotations

    在上期讨论中我们介绍了Scala Macros,它可以说是工具库编程人员不可或缺的编程手段,可以实现编译器在编译源代码时对源代码进行的修改.扩展和替换,如此可以对用户屏蔽工具库复杂的内部细节,使他们可 ...

  5. jQuery flickity 滑动触屏

    flickity是一款自适应手机触屏滑动插件,它的API参数很丰富,包括对齐方式.循环滚动.自动播放.是否支持拖动.是否开启分页.是否自适应窗口等. 在线实例 实例演示 使用方法 <div cl ...

  6. UIBezierPath-完善曲线

    override func draw(_ rect: CGRect) { let path = UIBezierPath() // 起点 path.move(to: CGPoint(x: , y: ) ...

  7. Android系统属性简介

    查看Android源码你会发现,代码中大量存在:SystemProperties.set()/SystemProperties.get():通过这两个接口可以对系统的属性进行读取/设置,顾名思义系统属 ...

  8. 一个基于Microsoft Azure、ASP.NET Core和Docker的博客系统

    2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客.当然,我写博客也不是从2008年才开始的,在更早时候,也在CSDN和系统分析员协会(之后名为“希赛网”)个人空间发布过一些 ...

  9. 如何dos中查看当前MySQL版本信息?

    dos中MySQL安装服务成功--启动服务成功--登录MySQL成功后输入:select version();  如图:

  10. SecondaryNameNode的工作流程

    SecondaryNameNode是用来合并fsimage和edits文件来更新NameNode和metadata的. 其工作流程为: 1.secondary通知namenode切换edits文件 2 ...