[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 ...
随机推荐
- nopi导入导出
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Ubuntu系统中安装RPM格式包的方法
Ubuntu的软件包格式为deb,而RPM格式的包则是Red Hat 相关系统所用的软件包.当我们看到一个想用的软件包时,如果他是RPM格式,而你的操作系统是Ubuntu,那岂不是很遗憾?其实,在Ub ...
- Oracle实用技巧
一. ORACLE SQL PLUS 使用技巧: ----①查找重复记录: SELECT DRAWING, DSNOFROM EM5_PIPE_PREFABWHERE ROWID!= (SELECT ...
- 为什么选择Typescript
上一节,我简单介绍了Typescript,并将Typescript和JavaScript进行了对比,有些网友提出了一些疑问,可能有些网友对于这个Typescript还不是特别的熟悉,这节,我做一些演示 ...
- Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化
图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...
- webViewDidFinishLoad 执行多次的问题
在做网页加载进度条的时候,发现UIWebViewDelegate中webViewDidFinishLoad方法会执行多次: - (void)webViewDidStartLoad:(UIWebView ...
- 【BZOJ1861】【splay】Book 书架
Description 小 T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿 ...
- linux 下面 opcache 拓展
PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展,只需要在编译安装的时候, 如果你使用--disable-all参数 禁用了默认扩展的构建, 那么必须使用--enable-opcach ...
- 再说CSS3渐变——线性渐变
渐变背景一直以来在Web页面中都是一种常见的视觉元素.但一直以来,Web设计师都是通过图形软件设计这些渐变效果,然后以图片形式或者背景图片的形式运用到页面中.Web页面上实现的效果,仅从页面的视觉效果 ...
- 简单学C——第一天
基本功 一.数据类型: 在C语言中,有数据类型这一说法.为何有这一说法?是因为在现实生活中存在着不同的数据,(例如整数,小数,字符即a b c d , . ; " 之类).由于计算机中所有 ...