博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Flip Game 翻转游戏
阅读量:7039 次
发布时间:2019-06-28

本文共 1005 字,大约阅读时间需要 3 分钟。

 

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
generatePossibleNextMoves(string s) { vector
res; for (int i = 1; i < s.size(); ++i) { if (s[i] == '+' && s[i - 1] == '+') { res.push_back(s.substr(0, i - 1) + "--" + s.substr(i + 1)); } } return res; }};

 

类似题目:

 

参考资料:

 

 

转载地址:http://rhtal.baihongyu.com/

你可能感兴趣的文章
2018 1.21测试
查看>>
DFS与BFS对比
查看>>
dedeCMS php语法在模版中的应用
查看>>
sublime 安装ctag 实现函数跳转
查看>>
sshd问题:A protocol error occurred. Change of username or service not allowed
查看>>
jQuery开发者眼中的AngularJS
查看>>
【DAY9】 关于多线程熊吃蜜Demo1的作业实验
查看>>
Python实现多属性排序
查看>>
nginx 访问日志分析
查看>>
RabbitMQ之消息确认机制(事务+Confirm)
查看>>
给出一个数组,计算数组中少了哪个数据的实现
查看>>
USB-232卡 配置
查看>>
C#窗体程序皮肤设置
查看>>
T-SQL.字符串函数
查看>>
mysql慢查询
查看>>
offices文件打开乱码问题如何处理
查看>>
抓屏程序
查看>>
many-to-many出现的问题
查看>>
第5章 配置邮箱服务
查看>>
node.js的一个简单框架
查看>>