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.

For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".

Follow up:
Derive your algorithm's runtime complexity.

思路:递归求解。

  1. class Solution {
  2. public:
  3. bool canWin(string s) {
  4. for (int i = ; i < s.size(); i++)
  5. if (s[i] == '+' && s[i-] == '+') {
  6. s[i] = s[i-] = '-';
  7. if (!canWin(s)) return true;
  8. s[i] = s[i-] = '+';
  9. }
  10. return false;
  11. }
  12. };

Flip Game II -- LeetCode的更多相关文章

  1. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  2. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  3. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  4. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  5. [LeetCode] Flip Game II 翻转游戏之二

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

  6. LeetCode Flip Game II

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

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

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

  8. LeetCode 294. Flip Game II

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

  9. 【LeetCode】294. Flip Game II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 日期 题目地址:https://leetc ...

随机推荐

  1. Response.Redirect在新窗口打开(转载)

    Response.Rederect在默认情况下是在本页跳转,所以除了在js中用window.open或是给A标签添加target属性之外,在后台似乎不能来打开新的页面,其实不然,通过设置form的ta ...

  2. DotNet 学习笔记 OWIN

    Open Web Interface for .NET (OWIN) ----------------------------------------------------------------- ...

  3. EditText中inputType详解

    <EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  4. 深入分析_linux_spinlock_实现机制【转】

    转自:http://blog.csdn.net/electrombile/article/details/51289813 在 x86 平台上,spinlock 主要通过处理器的 lock 指令前缀实 ...

  5. Kettle提高输入输出数据总结

    1 mysql在数据连接是可以通过设置一下三个三处的方式 useServerPrepStmts=false       useCursorFetch=true      useCompression= ...

  6. jQuery -《锋利的jQuery》————读后小结

    jQuery是一个优秀的javascript库. 我用的是vs2012自带的  jquery-1.8.2.js这个库,在Scripts这个文件夹下面 首先,我们使用jQuery要在head标签内引入j ...

  7. zookeeper安装和搭建集群方式(window)

    1.   概述 ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂 ...

  8. thinkphp5 消息队列thinkphp-queue扩展

    1.简介 thinkphp-queue是thinkphp的一个第三方扩展, 内置了 Redis,Database,Topthink ,Sync这四种驱动,推荐使用redis 2. 下载 和安装 com ...

  9. LightOJ - 1370

    Bi-shoe and Phi-shoe Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu S ...

  10. poj 3280(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 38 ...