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)
随机推荐
- [.Net Core] - 当 .Net Core 版本由 1.x 升级至 2.x 后,Cookie 使用方式变更
背景 Asp.Net Core 项目升级至 2.x 版本后,Cookie 验证方式需要进行更新. 升级前:.Net Core 1.x Startup.cs public void Configure( ...
- 开始使用 Manjaro(添加源+字体渲染去模糊+软件安装+优化配置+常见错误)(30)
1. 添加 archlinux 镜像源 1. 步骤一 向 /etc/pacman.d/mirrorlist 中添加国内镜像地址 1.1 方法1:自动添加 1. 输入如下命令查看国内镜像源,并按质量排序 ...
- 【Qt开发】第一个Qt程序Hello World!
一:说在前头 我的第一份工作是做生产工具,当时用的MFC,IDE是VC6.0,现在想想真是古董级别,10年至今,微软也一直没有对MFC进行升级,冥冥中感觉微软自己都放弃MFC了,市场上貌似MFC的岗位 ...
- 全能中间件 REST API 使用手册
全能中间件 REST API 使用手册 Ver:17.6.24 技术支持QQ:64445322 QQ群:339616649 任何第三方应用或网站都可以通过使用开放API为用户提供实时优质的服务. ...
- Python Threading 线程/互斥锁/死锁/GIL锁
导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 class ...
- PAT(B) 1044 火星数字(Java)进制转换
题目链接:1044 火星数字 (20 point(s)) 题目描述 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, ...
- PAT(B) 1072 开学寄语(Java)统计
题目链接:1072 开学寄语 (20 point(s)) 题目描述 下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使 ...
- Angular 学习笔记 ( 我追的 feature 和 bug )
Angular 有许多功能还不齐全,也有不少 bug 让人很头疼,所以这里做一些记入 Angular Bug 1.input type="number", valueChanges ...
- 关于Vue-elementUI中,给input手动赋值之后无法修改的问题解决
方案一:在data中给input的值赋一个初始值 方案二:在给input赋值时,使用this.$set
- 移动端隐藏滚动条,css方法
小白第一次发文记录自己遇到的问题. 关于隐藏移动端滚动条方法很多,这里只说本人用到的. 在PC端隐藏html右侧默认滚动条 html { /*隐藏滚动条,当IE下溢出,仍然可以滚动*/ -ms-ove ...