[Locked] Flip Game I & II
Flip Game I
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 []
.
分析:
一层处理即可
代码:
vector<string> flipGame(string str) {
vector<string> vs;
for(int i = ; i < str.length() - ; i++) {
if(str[i] == '+' && str[i + ] == '+') {
str[i] = str[i + ] = '-';
vs.push_back(str);
str[i] = str[i + ] = '+';
}
}
return vs;
}
Flip Game II
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 determine if the starting player can guarantee a win.
For example, given s = "++++"
, return true. The starting player can guarantee a win by flipping the middle "++"
to become "+--+"
.
Follow up:
Derive your algorithm's runtime complexity.
分析:
第一想法是找到合适的规律,可以直接从当前状态判断是否必胜,经过尝试,难以直接判断;所以采用一般解法,当两边都是没有失误的高手状态下,有以下规律:
1、终结点是必败点(P点);
2、从任何必胜点(N点)操作,至少有一种方法可以进入必败点(P点)
3、无论如何操作, 从必败点(P点)都只能进入必胜点(N点).
这里用到的就是1,2,3规律,对手无点可操作,则以失败终结;自己必胜,则至少一种方法进入下一轮必败点;若不能必胜,则找不到方法进入下一轮必败点;若自己必败,则无论用什么方法下一轮都是必胜点。
代码:
bool canwin(string str) {
for(int i = ; i < str.length() - ; i++) {
if(str[i] == '+' && str[i + ] == '+') {
str[i] = str[i + ] = '-';
int win = !canwin(str);
str[i] = str[i + ] = '+';
if(win)
return true;
}
}
return false;
}
[Locked] Flip Game I & II的更多相关文章
- Flip Game I && II
Flip Game I Problem Description: You are playing the following Flip Game with your friend: Given a s ...
- [Locked] Meeting Room I && II
Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...
- [Locked] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- sql语句优化总结
sql语句优化总结 数据库优化的几个原则: 1.尽量避免在列上做运算,这样会导致索引失败: 2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query.不然join的越 ...
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- LeetCode Flip Game II
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
随机推荐
- Oracle数据导入导出imp/exp命令总结
racle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. 利用 ...
- tableView嵌套collectionView
首先是自定义collectionView填充的tableViewCell import UIKit // 定义一个collectionView,重写初始化大小和布局方法 class TrendsDet ...
- UIView下使用Animation控制动画
转载自:http://blog.csdn.net/xunyn/article/details/8128031 动画效果是IOS界面重要的特色之一,其中CAAnimation是所有动画对象的抽象父类,作 ...
- wpf 窗体中显示当前系统时间
先看一下效果: 这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间. 接下来展示一下代码: 在XAML中: <StackPanel Width="205" ...
- pthread_setcanceltype 线程取消
取消线程: (1)一个线程可以调用pthread_cancel来取消另一个线程. (2)被取消的线程需要被join来释放资源. (3)被取消的线程的返回值为PTHREAD_CANCELED ...
- 记一个问题的AC
今天突然做一道LCT的染色问题的时候突然想到一个两个月前一道没有AC的题目. 链接 大意是,给一个长度为10^4的序列,最多有255个不同的数字,有最多10^5次方个询问,对于每个询问 l,r 输出[ ...
- 【模板】【凸包】Graham_scan
/* 唐代李白 <江夏别宋之悌> 楚水清若空,遥将碧海通.人分千里外,兴在一杯中. 谷鸟吟晴日,江猿啸晚风.平生不下泪,于此泣无穷. */ #include <iostream> ...
- MS SQL SERVER 2008 R2 实例服务启动出现10048错误解决办法
由于个人癖好,把MSSQLSERVER服务禁止了开机启动,每次需要的时候就输入CMD命令开启.今天在开启的时候,系统提示“发生服务特定错误:10048”. 于是打开SQL Server配置管理器,发现 ...
- phpexcel 一些基本的设置 (表格的基本属性)
网址是:http://www.thinkphp.cn/code/1893.html
- discuz二次开发技巧
discuz二次开发技巧 二次开发大多时候知识设置和处理,如果能够获知模板文件获得的变量数组将大大提高我们的开发效率 获取页面已经定义的变量 <--{eval printf_r(get_defi ...