[LeetCode] Flip Game 翻转游戏之二
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.
Example:
Input:s = "++++"
Output: true
Explanation: The starting player can guarantee a win by flipping the middle"++"to become"+--+".
Follow up:
Derive your algorithm's runtime complexity.
这道题是之前那道 Flip Game 的拓展,让我们判断先手的玩家是否能赢,可以穷举所有的情况,用回溯法来解题,思路跟上面那题类似,也是从第二个字母开始遍历整个字符串,如果当前字母和之前那个字母都是+,那么递归调用将这两个位置变为--的字符串,如果返回 false,说明当前玩家可以赢,结束循环返回 false。这里同时贴上热心网友 iffalse 的解释,这道题不是问 “1p是否会怎么选都会赢”,而是 “如果1p每次都选特别的两个+,最终他会不会赢”。所以 canWin 这个函数的意思是 “在当前这种状态下,至少有一种选法,能够让他赢”。而 (!canWin) 的意思就变成了 “在当前这种状态下,无论怎么选,都不能赢”。所以 1p 要看的是,是否存在这样一种情况,无论 2p 怎么选,都不会赢。所以只要有一个 (!canWin),1p 就可以确定他会赢。这道题从博弈论的角度会更好理解。每个 player 都想让自己赢,所以每轮他们不会随机选+。每一轮的 player 会选能够让对手输的+。如果无论如何都选不到让对手输的+,那么只能是当前的 player 输了,参见代码如下:
解法一:
class Solution {
public:
bool canWin(string s) {
for (int i = ; i < s.size(); ++i) {
if (s[i] == '+' && s[i - ] == '+' && !canWin(s.substr(, i - ) + "--" + s.substr(i + ))) {
return true;
}
}
return false;
}
};
第二种解法和第一种解法一样,只是用 find 函数来查找 ++ 的位置,然后把位置赋值给i,然后还是递归调用 canWin 函数,参见代码如下:
解法二:
class Solution {
public:
bool canWin(string s) {
for (int i = -; (i = s.find("++", i + )) >= ;) {
if (!canWin(s.substr(, i) + "--" + s.substr(i + ))) {
return true;
}
}
return false;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/294
类似题目:
Guess Number Higher or Lower II
参考资料:
https://leetcode.com/problems/flip-game-ii/
https://leetcode.com/problems/flip-game-ii/discuss/74033/4-line-Java-Solution
https://leetcode.com/problems/flip-game-ii/discuss/74010/Short-Java-and-Ruby
https://leetcode.com/problems/flip-game-ii/discuss/73962/Share-my-Java-backtracking-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Flip Game 翻转游戏之二的更多相关文章
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- LeetCode:将有序数组转换为二叉搜索树【108】
LeetCode:将有序数组转换为二叉搜索树[108] 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
随机推荐
- MAC终端命令行下用sublime、vscode、atom打开文件或目录
要知道,有时候一些小技巧,能极大的加大我们的工作效率. 在MAC下开发,用的最多的还是终端,我的终端环境是iterm2+ohmyzsh:步入正题前先给大家介绍几个小技巧: 第一个: 打开findle, ...
- 在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查
Sequelize是一个基于promise的关系型数据库ORM框架,这个库完全采用JavaScript开发并且能够用在Node.JS环境中,易于使用,支持多SQL方言(dialect),.它当前支持M ...
- C#开发微信门户及应用(12)-使用语音处理
我们知道,微信最开始就是做语音聊天而使得其更加流行的,因此语音的识别处理自然也就成为微信交流的一个重要途径,微信的开发接口,也提供了对语音的消息请求处理.本文主要介绍如何利用语音的识别,对C#开发的微 ...
- ASP.NET用QQ,网易发送邮件以及添加附件
教程:ASP.NET用QQ,网易发送邮件以及添加附件 这是我用QQ邮箱出现的异常: 命令顺序不正确. 服务器响应为:Error: need EHLO and AUTH first !无法从传输连接中读 ...
- python 添加tab补全
在平时查看Python方法用到tab补全还是很方便的. 1. mac 平台 配置如下: mac是类Unix平台,需要在添加一条配置内容到bash_profile 中(默认是没有这个文件,可以新建一个放 ...
- openresty 前端开发入门一
OpenResty ™ 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 ...
- spring源码:核心组件(li)
一.AOP实现 Spring代理对象的产生:代理的目的是调用目标方法时我们可以转而执行InvocationHandler类的invoke方法,所以如何在InvocationHandler上做文章就是S ...
- PHP 原型模式
原型模式:原型模式是先创建好一个原型对象,然后通过拷贝原型对象来创建新的对象.适用于大对象的创建,因为创建一个大对象需要很大的开销,如果每次new就会消耗很大,原型模式仅需内存拷贝即可.也可以用作动态 ...
- GreenDao2.2升级GreenDao3.0的适配之路
前言.为什么要升级到Greendao3.0? 1. 多人开发 以往的数据库建表建Dao等操作要新开一个module,在统一的地方管理数据库建表,现在可以直接写Entity.多人开发时自己管自己的Ent ...
- CALayer基本介绍与常见属性
属性框架:QuartzCore CA: CoreAnimation -> 核心动画,所有的核心动画都是添加给layer的! 与UIView的区别: 1.layer负责内容的展示,不接受任何用户交 ...