最后更新

一刷。

用数学方法是看是不是3的倍数。

不用数学方法的话要动态规划。

当前玩家,dp[i]行不行取决于dp[i-1]和dp[i-2],代表下一个玩家能不能赢,另一个玩家能赢的话当前就不能赢;另一个玩家不能赢,当前就能赢。

因为当前玩家可以通过拿1个或者拿2个来左右结果。

dp[i] = !dp[i-1] || !dp[i-2];

然后滚动数组滚起来。。

public class Solution {
public boolean firstWillWin(int n) {
if (n == 0) return false; boolean[] dp = new boolean[3];
dp[0] = true;
dp[1] = true;
dp[2] = false; for (int i = 3; i < n; i++) {
dp[i%3] = !dp[(i-1)%3] || !dp[(i-2)%3];
} return dp[(n-1)%3];
}
}

394. Coins in a Line的更多相关文章

  1. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  2. [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

    Description There are n coins with different value in a line. Two players take turns to take one or ...

  3. [LintCode] Coins in a Line II 一条线上的硬币之二

    There are n coins with different value in a line. Two players take turns to take one or two coins fr ...

  4. [LintCode] Coins in a Line 一条线上的硬币

    There are n coins in a line. Two players take turns to take one or two coins from right side until t ...

  5. LeetCode Coins in a Line

    There are n coins in a line. Two players take turns to take one or two coins from right side until t ...

  6. Lintcode394 Coins in a Line solution 题解

    [题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...

  7. Coins in a Line I & II

    Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...

  8. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  9. [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

随机推荐

  1. H5小内容(五)

    Geolocation(地理定位)   基本内容     地理定位 - 地球的经度和纬度的相交点     实现地理定位的方式       GPS - 美国的,依靠卫星定位       北斗定位 - 纯 ...

  2. HTML5 的绘图支持- canvas

    Canvas HTML5新增了一个canvas元素,它是一张空画布,开发者需要通过JavaScript脚本进行绘制. 在canvas上绘图,经过如下3步 (1) 获取canvas元素对应的DOM对象. ...

  3. PHP小记录

    正的framework(大量使用)      thinkphp(部分使用)      cakephpyii(极少使用) [一]函数    1:函数的声明:每个函数的第一行都是函数开头,有声明函数的关键 ...

  4. php中浮点数计算问题

    如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...

  5. php中mysqli 处理查询结果集的几个方法

    最近对php查询mysql处理结果集的几个方法不太明白的地方查阅了资料,在此整理记下 Php使用mysqli_result类处理结果集有以下几种方法 fetch_all() 抓取所有的结果行并且以关联 ...

  6. Python冒泡排序

    冒泡排序,顾名思义,按照一定的规则,把数据一直排下去 直接上代码 import random def bubblesort(data): for i in range(len(data)-1,1,-1 ...

  7. Listview 加载更多

    JQM Listview 加载更多 demo - Warren的个人主页 JQM Listview 加载更多 Demo 测试数据1 测试数据2 测试数据3 测试数据4 显示更多 Page Footer ...

  8. PDF判断打印是A4还是B5

    打印材料通畅就是这样两个规格,之前经常受其困扰,B5规格达成A4会显得字很大,当然本身A4就跟大:如果是A4打成B5字很小的: 其实,判断依据就是Adobe reader里面的,当鼠标滑向左下角的时候 ...

  9. iOS开发之iOS程序偏好设置(Settings Bundle)的使用

    目录[-] 1.添加设置项 2.设置的控件 3.编辑设置项的文件 4.在程序中获取Settings 和写入Settings 添加UI 5.实现读取设置和保存代码 在Android手机上, 在某个程序里 ...

  10. 文档学习 - UILabel - 属性详解

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...