LeetCode 294. Flip Game II
原题链接在这里:https://leetcode.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.
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.
题解:
若是有一段"++", 剩下的段和"--"组合 can not win, 那么返回true.
从头到尾试遍了没找到这么一段"++", 返回false.
Time Complexity: exponential.
T(n) = (n-2) * T(n-2) = (n-2) * (n-4) * T(n-4) = O(n!!)
AC Java:
public class Solution {
public boolean canWin(String s) {
for(int i = 1; i<s.length(); i++){
if(s.charAt(i) == '+' && s.charAt(i-1) == '+' && !canWin(s.substring(0, i-1) + "--" + s.substring(i+1))){
return true;
}
}
return false;
}
}
类似Flip Game.
LeetCode 294. Flip Game 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(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- 【LeetCode】294. Flip Game II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 日期 题目地址:https://leetc ...
- 294. Flip Game II
题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...
- [LeetCode] 293. Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode 293. Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
随机推荐
- linux学习之路(二)--centos7安装Redis(单点)
一.安装redis 1.进入/usr/local/,新建services目录,进入该目录,下载redis wget http://download.redis.io/releases/redis-4. ...
- RuntimeError: Model class users.models.UserProfile doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Django启动的时候报错 File "/home/hehecat/PycharmProjects/MxShop/MxShop/urls.py", line 23, in from ...
- 从零开始搭建一个简单的基于webpack的vue开发环境
原文地址:https://segmentfault.com/a/1190000012789253?utm_source=tag-newest 从零开始搭建一个简单的基于webpack的react开发环 ...
- C++复制构造函数,类型转换构造函数,析构函数,引用,指针常量和常量指针
复制构造函数形如className :: className(const &) / className :: className(const className &)后者能以常 ...
- Django_rest_framework分页
分页基本流程及配置 1.基于LimitOffsetPagination做分页,根据配置 from rest_framework.pagination import LimitOffsetPaginat ...
- ORA-01790 错误处理 SQL同一数据库中,两个查询结果数据类型不同时的union all 合
转自: 出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同. 所以在union 或者union all 的时候造成了 ...
- USB原理简单叙述
USB简介: USB的几种版本: 1. USB 1.0:速度 1.5Mb/s 2. USB 1.1:速度 12Mb/s 3. USB 2.0:速度 60MbB/s 4. USB 3.0:速度 640M ...
- Windows下常用DOS命令
1.添加用户命令: net user 用户名 密码 /add 2.将用户加入组的命令: net localgroup administrators 用户名 /add 3.在dos命令行模式下启用用户: ...
- [#Linux] CentOS 7 美化调优
优化美化系统,是为了让新系统能更顺眼顺手,符合自己过去在windows下的使用习惯,从而实现平稳过渡. 正如开篇时谈到的,现在的桌面版linux已相当友好(特别是Ubuntu),基本不需要做什么额外设 ...
- [#Linux] CentOS 7 禁用笔记本的触摸板
安装 xorg-x11-apps yum install xorg-x11-apps 查看对应设备的 id xinput –list 关闭 touchpad xinput set-int-prop 1 ...