294. 翻转游戏 II
题目:
链接:https://leetcode-cn.com/problems/flip-game-ii/
你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串。你和朋友轮流将 连续 的两个 "++" 反转成 "--"。 当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。
请你写出一个函数来判定起始玩家是否存在必胜的方案。
示例:
输入: s = "++++"
输出: true
解析: 起始玩家可将中间的 "++" 翻转变为 "+--+" 从而得胜。
延伸:
请推导你算法的时间复杂度。
还是太菜了,中等题目都做不出来。
方法:就是递归回溯,复杂度n^n(不确定)
传值的:
class Solution {
public:
bool canWin(string s) {
for(int i = ; i < s.size(); ++i){
if(i + < s.size() && s[i] == '+' && s[i + ] == '+'){//找任何一种方法让对方赢不了,我就赢了
string tmp = s;
tmp[i] = '-', tmp[i + ] = '-';
if(!canWin(tmp)){
return true;
}
}
}
return false;
}
};

传引用的:
class Solution {
public:
bool canWin(string& s) {
if (!s.length())
return false;
int ptr = ;
while (ptr < s.length() - ) {
if (s[ptr] == s[ptr + ] && s[ptr] == '+') {
s[ptr] = s[ptr + ] = '-';
if (!canWin(s)) {
s[ptr] = s[ptr + ] = '+';
return true;
}
s[ptr] = s[ptr + ] = '+';
}
ptr++;
}
return false;
}
};

记忆化搜索(用map保存找过的情况):
class Solution {
public:
unordered_map<string, bool> memo;
bool canWin(string s) {
if (memo.count(s)) return memo[s];
for (int i = ; i < s.size(); ++i) {
if (s[i] == '+' && s[i - ] == '+') {
string t = s;
t[i] = t[i - ] = '-';
if (!canWin(t)) {
memo[s] = true;
return true;
}
}
}
memo[s] = false;
return memo[s];
}
};

294. 翻转游戏 II的更多相关文章
- [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] 293. Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- XDU 1161 - 科协的数字游戏II
Problem 1161 - 科协的数字游戏II Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 112 ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...
- [Swift]LeetCode293. 翻转游戏 $ Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- Luogu 1764 翻转游戏 - 枚举 + 搜索
题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...
- HihoCoder - 1615矩阵游戏II(贪心)
矩阵游戏II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个NxN的整数矩阵,小Hi每次操作可以选择两列,将这两列中的所有数变成它的相反数. 小Hi可以进行任意 ...
- 293. Flip Game只翻转一步的加减号翻转游戏
[抄题]: You are playing the following Flip Game with your friend: Given a string that contains only th ...
随机推荐
- 安装Gitlab到Ubuntu(APT)
运行环境 系统版本:Ubuntu 16.04.6 LTS 软件版本:Gitlab-ce-11.10.1 硬件要求:最低2核4GB,建议4核8GB 安装过程 1.安装依赖 root@localhost: ...
- python 3.6 安装 opencv 3.4
一种说法是,到opencv官网下载相应的版本opencv,解压,把cv2.pyd放到 python安装文件夹下的\Lib\site-packages里即可, 此时import cv2即可成功 我的没有 ...
- adb -- cannot connect to x.x.x.x:5555“由于目标计算机积极拒绝,无法连接”
原因 安卓系统未打开adb网络调试功能 通过USB方式连接到安卓系统设置即可 解决 先通过USB线连接 adb devices 能看到所连接的设备情况下 adb root 权限提权 adb shell ...
- oracle备份与还原数据
一.表数据备份与还原 creat table 备份表 select * from 原表 where insert into 原表 select * from 备份表 二.利用备份 ...
- Laravel框架中通过EasyWeChat发送公众号模板消息
环境要求 PHP >= 7.0 PHP cURL 扩展 PHP OpenSSL 扩展 PHP SimpleXML 扩展 PHP fileinfo 拓展 使用composer安装: $ compo ...
- Linux内核提权漏洞(CVE-2019-13272)
漏洞描述 kernel / ptrace.c中的ptrace_link错误地处理了想要创建ptrace关系的进程的凭据记录,这允许本地用户通过利用父子的某些方案来获取root访问权限 进程关系,父进程 ...
- 安装Kibana到Linux(源码)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:Kibana-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK 1.1.从官网 ...
- PTA 1001 A+B Format
问题描述: Calculate a+b and output the sum in standard format -- that is, the digits must be separated i ...
- 小白月赛22 J : 计算 A + B
J:计算 A + B 考察点 : 高精度,字符串 坑点 : 字符串中可能全是数字,或者 + 超过 1 个,需要进行特殊判断 析题得侃: 关于高精度的各种板子 Code: #include <ve ...
- 小白的java学习之路 “ 字符串”
定一个字符串可以使用string类和stringbuffer类. string类提供了大量的操作字符串的方法,常用的如下: 获得字符串的长度: length(). 比较字符串:equals(). 链接 ...