LeetCode 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 Flip Game II的更多相关文章
- [LeetCode] Flip Game 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] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/ 题目: You are playing the following Flip Game with yo ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
随机推荐
- Json 数组拼接
var str1 = {"name": "apple", "sex": "21"}; / ...
- python模块之codecs
http://blog.csdn.net/suofiya2008/article/details/5579413
- 使用Expression做Linq的參數化排序
Linq非常的好用,減少大量的資料庫操作手序,使用具名的類別,減少了在程式中寫SQL寫錯字的可能性,問題來了,如果我想用QueryString中的參數,作為排序的依據,但是因為是具名的類別,不能指定字 ...
- jstl fn标签
JSTL使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请 求参数或者context以及session中的数据的访问非常方便,但是我们在实际应用中经常需要 ...
- 第五次实验报告 java 网络编程
20145306 第五次 java 实验报告 实验内容 客户端与服务器连接,客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值,一起传送给客 ...
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- oracle sqlplus常用命令
登录到sqlplus sqlplus user/pwd@dbname 不登录使用 sqlplus /nolog 查看当前登录用户 show user; 更改用户密码 ALTER USER USER I ...
- MySQL字段之集合(set)枚举(enum)
MySQL字段之集合(set)枚举(enum) (2008-12-23 13:51:23) 标签:it 分类:MySQL 集合 SET mysql> create table jihe(f1 ...
- Decomposing and Redistributing the Statement Method
Refactoring: Improving the Design of Existing Code Decomposing and Redistributing the Statement Meth ...
- DML以及DQL的使用方法
DML:数据操作语言 1.插入insert into 单行插入:insert into 表名 (字段名, 字段名,...) values (值, 值, ...) 注:值列表要和字段列表相匹配. ins ...