[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 ...
随机推荐
- WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例
说明:1.后台代码添加测试 数据 2.使用 richTextBox.ScrollToVerticalOffset()方法,滚动竖直方向滚动条位置 3.使用定时器DispatcherTimer,修改页面 ...
- c#迭代算法
//用迭代算法算出第m个值 //1,1,2,3,5,8...; //{1,0+1,1+1,1+2,2+3 ,3+5} static void Main(string[] arg ...
- Android开发手记(26) Java多线程的实现
随着多核CPU的发展,多线程编程显得越来越重要,本文将对Java中的多线程编程进行一些简单的探讨. 1.继承Thread类 Java中,线程运行的基本单位是Thread,所以,我们可以通过继承Thre ...
- javascript ~~ 符号的使用
其实是一种利用符号进行的类型转换,转换成数字类型 大概是这样滴: ~~true == 1 ~~false == 0 ~~"" == 0 ~~[] == 0 ~~undefined ...
- Cookie技术详解
1. Cookie的特性 属性: 1> name: Cookie的名字 2> value: Cookie的值 3> path: 可选,Cookie的存储路径,默认情况下的存储路径时访 ...
- javascript——可以判断值的类型的函数
function classof(o){ return Object.prototype.toString.call(0).slice(8,-1); } Function.prototype.getN ...
- 算法系列之图--DFS
深度优先搜索使用的策略是,只要与可能就在图中尽量“深入”.DFS总是对最近才发现的结点v出发边进行探索,知道该结点的所有出发边都被发现为止.一旦v的所有出发边都被发现了,搜索就回溯到v的前驱结点(v是 ...
- 从SQL2008R2导入数据到SQL2005
从SQL2008R2导入数据到SQL2005,数据很大,数据文件大概有120G. 尝试过直接离线附加,失败 尝试过备份还原,失败 最后找到了 1.先执行 exec sp_msforeachtable ...
- Python自动化运维之23、Dom
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.最为关心的是,DOM把网页 ...
- 全国省市区Json文件 ,做省市区联动很轻松
省份 [{"name":"安徽省", "code":"340000"},{"name":" ...