题目:

链接: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的更多相关文章

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

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

  2. [LeetCode] 293. Flip Game 翻转游戏

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

  3. XDU 1161 - 科协的数字游戏II

    Problem 1161 - 科协的数字游戏II Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 112  ...

  4. lintcode: 跳跃游戏 II

    跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A =  ...

  5. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  6. [Swift]LeetCode293. 翻转游戏 $ Flip Game

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

  7. Luogu 1764 翻转游戏 - 枚举 + 搜索

    题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...

  8. HihoCoder - 1615矩阵游戏II(贪心)

    矩阵游戏II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个NxN的整数矩阵,小Hi每次操作可以选择两列,将这两列中的所有数变成它的相反数. 小Hi可以进行任意 ...

  9. 293. Flip Game只翻转一步的加减号翻转游戏

    [抄题]: You are playing the following Flip Game with your friend: Given a string that contains only th ...

随机推荐

  1. [Python]scatter_matrix报错 module 'pandas' has no attribute 'scatter_matrix'

    运行pandas.scatter_matrix()散点图函数时报错, 原因是该函数在新版本用法发生了变化: pandas.plotting.scatter_matrix 完整用法:pd.plottin ...

  2. layui 弹出层layer中from初始化 ,并在btn中返回from.data

    1.弹出对话框 layer.open() 来初始化弹层 // 监听添加操作 $(".data-add-btn").on("click", function () ...

  3. IDEA 同时打开两个项目,相互引用

  4. JN_0011:改变PPT的页面尺寸,并导出图片

    1,改变尺寸 设计 --  幻灯片大小 --  自定义大小 2,导出图片 另存为 JPG 图片

  5. Uva1213(线性筛模板+dp)

    题意: 把n拆成k个不同素数的和,有多少种拆法. 解法: 打表后dp即可,这个dp的问题可以归纳为:在n个数中选k个数,使得和m的方案数 #include<cstdio> #include ...

  6. JSP页面取不到ModelAndView里面存的值

    方法1:在jsp页面上加上<%@ page isELIgnored="false" %>

  7. mybatis-plus invalid bound statement (not found) insert解决办法

    使用mybatis-plus时,使用IService.insert方法时,提示找不到insert方法,原因是,mybatis-plus提供了两个BaseMapper和IService. 改成引用imp ...

  8. jquery click事件中的return false

    提交表单数据时设定了type="submit"属性,单击提交按钮后会默认刷新页面 但是在使用jquery的click事件时没出现跳转 $('button').click(funct ...

  9. ASP.NET Identity系列教程-4【Identity高级技术】

    https://www.cnblogs.com/r01cn/p/5194257.html 15 ASP.NET Identity高级技术 In this chapter, I finish my de ...

  10. 【巨杉数据库SequoiaDB】巨杉数据库无人值守智能自动化测试实践

    刚刚过去的春节,新型冠状病毒疫情突如其来地横扫大江南北.为了响应国家号召,许多软件公司和互联网公司也将在较长一段时间内建议员工采取远程办公的方式,同时也存在骨干工程师无法及时返岗的问题,使得生产力大受 ...