You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

Hint:

  1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?

思路:

当heap中有1、2、3个stones时一定获胜,当有4个stones时一定会输。那么当有5个stones的时候,我们可以先拿出来一个stones,此时相当于我们面对4个stones时的情形。

依次可以类推出,当heap中恰好有4N(N=1,2,3......)个stones时,我方一定会输。

java代码:

public class Solution {
public boolean canWinNim(int n) {
if(n%4==0) return false;
else return true;
}
}

LeetCode (262):Nim Game的更多相关文章

  1. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  3. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  4. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  5. 【算法功底】LeetCode 292 Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. LeetCode 292 Nim Game 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  7. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  8. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  9. LeetCode: 292 Nim Game(easy)

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

随机推荐

  1. 浏览器中使用 ES6 import

    html 中的 head 标签引入: <script src="test.js" type="module"></script> tes ...

  2. SharePoint Managed Metadata 使用总结

    前言 本文完全原创,转载请说明出处,希望对大家有用. 在SharePoint开发中,通常我们会将数据存储在列表,文档库或者直接存到数据库.但涉及到数据的层级结构时,用列表等存储实现并不是一件简单的事情 ...

  3. 后台程序在向tty/串口写数据的时候stop了

    当后台程序向tty/串口写数据的时候stop了. STOPPED(SIGTTOU) .... SIGTTOU:代表的是后台程序向 controlling terminal写数据. 解决办法:暂时在程序 ...

  4. iwconfig

    解决办法:清空/var/lib/dhclient/dhclient.leases文件里的所有内容 # sudo dhclient -r //release ip 释放IP # sudo dhclien ...

  5. Webpack4.x 入门

    概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...

  6. python 中几个层次的中文编码.md

    转自:[http://swj.me/] 介绍 一直不太喜欢使用命令行,所以去年年底的技术创新中,使用TkInter来开发小工具.结果花费了大量的时间来学习TkInter ui的使用. 最近想整理该工具 ...

  7. Ta-lib K线模式识别

    1, CDL2CROWS (Two Crows 两只乌鸦) 简介:三日K线模式,第一天长阳,第二天高开收阴,第三天再次高开继续收阴,收盘比前一日收盘价低,预示股价下跌. 例子:integer = CD ...

  8. Yii框架2.0 数据库操作初接触

    Yii2.0和Yii1.1版本的变动还是挺多的,我发现配置文件有许多不同,Yii1.1版本里有个main.php 好多信息是在这里配置的,比如默认控制器,数据库连接信息:Yii的数据库配置被单独拿出来 ...

  9. javaScript 调用构造函数 Array() 时没有使用参数, length总是0

    如果调用构造函数 Array() 时没有使用参数,那么返回的数组为空,length 字段为 0. 当调用构造函数时只传递给它一个数字参数,该构造函数将返回具有指定个数.元素为 undefined 的数 ...

  10. 19.Eclipse 修改默认的keystore签名文件

    Android开发中apk运行都需要签名,就算连接手机直接运行调试,apk都有签名,开发工具会有默认的debug_keyStore Eclipse ADT调试运行使用的是临时生成的Debug专用证书, ...