[LeetCode] Flip Game 翻转游戏
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
类似题目:
参考资料:
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 翻转游戏的更多相关文章
- [LeetCode] 293. Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Flip Game 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] 294. Flip Game II 翻转游戏 II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [Swift]LeetCode293. 翻转游戏 $ Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- 【2018寒假集训 Day1】【位运算】翻转游戏
翻转游戏(flip) [问题描述] 翻转游戏是在一个 4 格×4 格的长方形上进行的,在长方形的 16 个格上每 个格子都放着一个双面的物件.每个物件的两个面,一面是白色,另一面是黑色, 每个物件要么 ...
- Luogu 1764 翻转游戏 - 枚举 + 搜索
题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...
- 294. 翻转游戏 II
题目: 链接:https://leetcode-cn.com/problems/flip-game-ii/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...
- NC235250 牛可乐的翻转游戏
NC235250 牛可乐的翻转游戏 题目 题目描述 牛可乐发明了一种新型的翻转游戏! 在一个有 \(n\) 行 \(m\) 列的棋盘上,每个格子摆放有一枚棋子,每一枚棋子的颜色要么是黑色,要么是白色. ...
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
随机推荐
- 【分布式】Zookeeper会话
一.前言 前面分析了Zookeeper客户端的细节,接着继续学习Zookeeper中的一个非常重要的概念:会话. 二.会话 客户端与服务端之间任何交互操作都与会话息息相关,如临时节点的生命周期.客户端 ...
- WCF Basics - FAQs Series【WCF基础----问答系列教程】
WCF学习系列一[WCF Interview Questions-Part 1 翻译系列] WCF学习系列二---[WCF Interview Questions – Part 2 翻译系列] WCF ...
- 服务发现与健康监测框架Consul-DNS转发的应用
关于Consul Consul是一个提供服务注册与发现,健康监测,Key/Value存储以及多数据中心存储的分布式框架.官网地址是https://www.consul.io/,公司初步应用后我们老大觉 ...
- java web学习总结(二十三) -------------------编写自己的JDBC框架
一.元数据介绍 元数据指的是"数据库"."表"."列"的定义信息. 1.1.DataBaseMetaData元数据 Connection.g ...
- shell笔记
shell:俗称操作系统的"外壳",就是命令解释程序. 是用户与Linux内核之间的接口. 是负责与用户交互,分析.执行用户输入的命令,并给出结果或出错提示. ...
- Java操作wkhtmltopdf实现Html转PDF
做java开发的都知道,java生成pdf大部分都是用itext,itext的确是java开源组件的第一选择.不过itext也有局限,就是要自己写模版,系统中的表单数量有好几百个,为每个表单做一个导出 ...
- javascript 练习示例(一)
confirm 点确定返回true,点取消返回false prompt 点确定返回用户输入的字符串,点取消返回null 判断奇偶性 var isOdd = prompt('请输入你得的数字'); if ...
- 慎用mutableCopy
因为逻辑需要,我在present到一个页面时,将一个存放uiimage的数组mutablecopy了过去(因为再返回的时候防止对数组做了改动),时间长了也忘了这事儿,后来发现添加多张图片上传时,app ...
- Oracle工具类-生成数据库现有Job的创建脚本
生成Oracle数据库现有Job的创建脚本 -- 生成现有Job的创建脚本 create or replace procedure proc_generate_job_create_sql is be ...
- java、easyui-combotree树形下拉选择框
最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...