【LeetCode】294. Flip Game II 解题报告 (C++)
- 作者: 负雪明烛
 - id: fuxuemingzhu
 - 个人博客:http://fuxuemingzhu.cn/
 
题目地址: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++)的更多相关文章
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
 - 【LeetCode】90. Subsets II 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
 - [LeetCode] 294. Flip Game II 翻转游戏 II
		
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
 - 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 ...
 - LeetCode 294. Flip Game II
		
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
 - 【LeetCode】275. H-Index II 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...
 - 【LeetCode】52. N-Queens II 解题报告(Python & C+)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...
 - 【LeetCode】454. 4Sum II 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
 - LeetCode: Pascal's Triangle II  解题报告
		
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
 
随机推荐
- 《python编程从入门到实践》读书实践笔记(一)
			
本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...
 - python包之drmaa:集群任务管理
			
目录 1. drmaa简介 2. 安装和配置 3. 示例 3.1 开始和终止会话 3.2 运行工作 3.3 等待工作 3.4 控制工作 3.5 查询工作状态 4. 应用 4.1 写一个简单应用 4.2 ...
 - R语言与医学统计图形-【27】ggplot2图形组合、字体、保存
			
ggplot2绘图系统--图形组合.字体选择.保存输出 1.图形组合 一页多图在基础包中利用par和layout函数来切分画布. ggplot2是先铺好网格背景,再进行绘图,所以要通过切分网格背景来实 ...
 - mysql—Linux系统直接进入mysql服务器,并实现一些基础操作
			
首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql进程列表 如果mysql未启动,你可以使用以下命令 ...
 - mac 下 如何在同一窗口打开多个终端并实现快捷键切换
			
相信大家编代码的时候都会遇到,每次需要在头文件,库文件和源码文件中编代码的时候,总是需要在几个文件中切换来切换去的,而且一个文件就一个终端窗口,每次都要用鼠标点来点去,非常麻烦,所以如果能把这几个文件 ...
 - cp -拷贝文件出现错误
			
对于cp -a最主要的用法是在保留原文件属性的前提下复制文件. 如果出现了拷贝文件错误,在文件前面加上-a 即可
 - kubernetes部署 flannel网络组件
			
创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...
 - 学习java 6.29
			
今天是学习Java的第一天. 学习内容:了解了JDK的下载和安装: 学会了如何配置Path环境变量及安装eclipse: 执行了HelloWorld案例: 在Java中关键字需要小写,Java中最基本 ...
 - HelloWorldMBean
			
package mbeanTest; public interface HelloWorldMBean { public String getHello(); public void setHello ...
 - Spring Cloud声明式调用Feign负载均衡FeignClient详解
			
为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...