【LEETCODE】模拟面试-294.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 twoconsecutive "++" 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.
input: a string with only + and -
two kinds of characters
output: for a given input, based on the game rule, return whether there is one strategy that can make the first player win, if yes, return true, if there is none, return false
corner: when the string if null, return false
We will first change the string to a character array.
And apply Backtracking
algorithm to solve it.
The starting point of the first player will iterate from 0 to arr.length - 2
, when he finds two consecutive ++
, he will soon change it into --
.
Then the second player will do the same thing, so we pass current changed array to the second player, and he will receive a result: boolean otherWin = helper(arr);
If the second player will win, means he will return a true
to upper level, the first player should recover his current strategy by change the --
to original ++
, and keep moving i
to next step, so that he can find other strategy that will make himself finally win the game, as long as there is just one strategy make it happen, the final result will be true, otherwise, it will be false.
public class Solution{
public boolean canWin(String s){
//corner
if ( s == null || s.length() == 0 ){
return false;
}
return helper(s.toCharArray());
}
public boolean helper(char[] arr){
for ( int i = 0; i < arr.length; i++ ){
if ( arr[i] == '+' && arr[i + 1] == '+' ){
arr[i] = '-';
arr[i + 1] = '-';
boolean otherWin = helper(arr); //2人轮流
arr[i] = '+'; //返回到upper level后恢复+号
arr[i + 1] = '+';
if ( !otherWin ){ //另一人false时走这一步,另一人true时,要继续遍历++对
return true; //直到找到某一种走法可以让另一人false,最终整体返回的值为true,此时第一人赢
}
}
}
return false; //如果遍历到头没有++对了,而且几种走法都一直找不到赢的走法了,第一人就最终false
}
}
- vmware安装ubuntu12.04嵌套安装xen server(实现嵌套虚拟化)
- Golang语言切片slice的线程协程安全问题
- ASP.NET Core 在 Azure 开启 HTTPS
- 算法基础:最大递减数问题(Golang实现)
- 亲身经历的痛--database/sql: Stmt的使用以及坑
- Ubuntu上通过nginx部署Django笔记
- Go学习笔记:golang交叉编译
- Python魔术方法-Magic Method
- python类中super()和__init__()的区别
- Python正则表达式:最短匹配
- 转--Go时间格式化和类型互换操作
- Python标准库(1) — itertools模块
- Linux笔记:使用Vim编辑器
- 一步一步学lucene——(第二步:示例篇)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法