题目地址:https://leetcode-cn.com/problems/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.

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.

题目大意

你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串。你和朋友轮流将 连续 的两个 “++” 反转成 “–”。 当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。
请你写出一个函数来判定起始玩家是否存在必胜的方案。

解题方法

记忆化搜索

类似的两个人轮流用同样的规则做游戏的题目,一般都可以用递归解决。

这个题使用递归的方法也很简单,第一个人在翻转++的时候,把翻转后的字符串递归传给下一个人。如果下一个人不能获胜,那么自己就获胜了。道理很简单。

如果不使用map保存已经判断过是否能获胜的字符串,总耗时540ms。但如果使用了map保存已经判定过的,可以获胜的字符串,那么时间缩短为52ms。这体现了记忆化搜索避免了重复的搜索,带来的高效。

C++代码如下:

class Solution {
public:
bool canWin(string s) {
const int N = s.size();
if (N <= 1) return false;
if (win_.count(s) && win_[s]) return true;
for (int i = 0; i < N - 1; ++i) {
if (s[i] == '+' && s[i + 1] == '+') {
string flip = s.substr(0, i) + "--" + s.substr(i + 2);
if (!canWin(flip)) {
win_[s] = true;
return true;
}
}
}
return false;
}
private:
unordered_map<string, bool> win_;
};

日期

2019 年 9 月 21 日 —— 莫生气,我若气病谁如意

【LeetCode】294. Flip Game II 解题报告 (C++)的更多相关文章

  1. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  3. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  5. LeetCode 294. Flip Game II

    原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...

  6. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

  7. 【LeetCode】52. N-Queens II 解题报告(Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...

  8. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  9. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

随机推荐

  1. Secant 方法求方程多个根

    Secant 方法介绍 Secant Method 函数 Secant_Methods 简介 1.函数定义 [c, errColumn] = Secant_Method(f, a, b, N, con ...

  2. 33. Implement strStr()

    http://blog.csdn.net/justdoithai/article/details/51287649 理解与分析 Implement strStr() My Submissions Qu ...

  3. linux命令行快速统计文件(压缩文件)的行数

    统计(文件|压缩文件)的行数 zcat file.gz | sed -n '$='                                         #迅速.直接打印出多少行.-n 取消 ...

  4. Scrapy-Splash的安装和使用

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Docker,安装之后,会启动一 ...

  5. 使用Mybatis出现的问题+配置优化+ResultMap

    一.可能出现的问题 1.Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ...

  6. linux RPM/YUM包管理

    linux RPM/YUM包管理 目录 linux RPM/YUM包管理 RPM RPM包管理 查询rpm包 卸载rpm包 安装rpm包 YUM 查看yum服务器是否有需要安装的软件 下载安装指定的y ...

  7. linux 内存变量的分布

    我们知道,linux通过虚拟内存管理进程的内存(进程的地址空间),而进程的地址空间分布如下 : 从进程的空间中可以看出,内存中的变量有的来自可执行elf文件,在elf文件中已经分配好存储空间,有的是在 ...

  8. 时间同步之pxe,cobbler,dhcp

    ntpdate 时间同步 同步方法 ntpdate ntp服务器IP 例: ntpdate 192.168.37.11 自动运行同步时间脚本 crontab -e * */1 * * * /usr/s ...

  9. Spring Boot中使用Servlet与Filter

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...

  10. Spring Boot中使用Mybatis

    一.步骤 导入依赖:MySQL驱动.Druid依赖.MyBatis与Spring Boot整合依赖.Lombok依赖 在Service接口实现类上添加@Service注解 在Dao接口上添加@Mapp ...