[leetcode] 293. Flip Game 解题报告
2016-03-11 14:41
363 查看
题目链接:https://leetcode.com/problems/flip-game/
You are playing the following Flip Game with your friend: Given a string that contains only these two characters:
you and your friend take turns to flip two consecutive
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
If there is no valid move, return an empty list
思路:只要判断连续的是不是"++"就行了,然后将其加入结果中去.
代码如下:
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
[].
思路:只要判断连续的是不是"++"就行了,然后将其加入结果中去.
代码如下:
class Solution { public: vector<string> generatePossibleNextMoves(string s) { vector<string> result; int len = s.size(); for(int i = 0; i < len-1; i++) if(s[i]=='+' && s[i+1]=='+') result.push_back(s.substr(0,i)+"--"+s.substr(i+2)); return result; } };
相关文章推荐
- Xcode7.0.1 / OS X 10.11.1 提交不了 AppStore 的解决方案
- Audiophobia uva10048
- Xcode 证书问题引起的错误详解
- python学习笔记四--类与模块
- 【转】unity面试题(问题篇)
- C#的对象内存模型
- 趣拍SDK接入问题Android
- 程序员应该具备的技能
- [C]指针有什么好处?
- 数据存储之(四): 归档、反归档
- 打包测试 之企业证书安装后信任问题
- java学习之消费者和生产者问题
- 浏览器兼容性第一弹
- 三种排序方法的比较
- 修改linux网卡名称
- 冒泡法排序C++实现
- PHP+shell脚本操作Memcached和Apache Status的实例分享
- 软件基础知识的补充资源
- mysql found_rows函数
- 打包测试 之 企业证书的打包过程以及注意事项(in-house方式)