LeetCode——Nim Game
Description:
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.
有一堆石头,两个人轮流取,一次只能取1~3个,取得最后一个石头的人赢。给一个整数n判断是不是能赢。
这个问题其实是一个著名的博弈论的问题,但是这个地方有点简单,列出来前12个答案就能找出规律,就是只要是4的倍数都不可能赢。
Nim Game:http://www.guokr.com/blog/777525/ http://www.cnblogs.com/exponent/articles/2141477.html
代码:
public class Solution {
public boolean canWinNim(int n) {
return !(n%==);
}
}
LeetCode——Nim Game的更多相关文章
- [LeetCode] Nim Game 尼姆游戏
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode: Nim Game
这题其实不太好想,用到了博弈论 1,2,3 能赢 4 输 5,6,7 赢 8 输 9,10,11 赢 12 输 那么结论就是4的倍数就是输,其他情况就能赢. 为什么会是这样呢?很好解释,根源就在4会输 ...
- LeetCode Nim Game (简单nim)
题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数, ...
- 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)(转)
最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...
- LeetCode 292. Nim Game (取物游戏)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- 【一天一道LeetCode】#292. Nim Game
一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)
最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...
- 【LeetCode】292. Nim Game 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- (转)TCP连接异常断开检测
TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现.某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接.下面介绍一种方法来检测这种异常断 ...
- eclipse调用jni
http://blog.chinaunix.net/uid-27003388-id-3235189.html 1. 在Eclipse里创建一个Java project(jni_test): ...
- MySQL binlog日志操作详解
MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的. bi ...
- Java常量池的理解
1.常量池的好处常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现了对象的共享.例如字符串常量池,在编译阶段就把所有的字符串文字放到一个常量池中.(1)节省内存空间:常量池中所有相同的字符串常 ...
- 移动端控制在input里输入的值只能是数字
<input type='text' oninput="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,''); ...
- Struts2/XWork 安全漏洞及解决办法
exploit-db网站在7月14日爆出了一个Struts2的远程执行任意代码的漏洞. 漏洞名称:Struts2/XWork < 2.2.0 Remote Command Execution V ...
- Objective-C语法之NSDictionary和NSMutableDictionary
Java 有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷.在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也 ...
- 【VirtualBox】共享文件夹失效问题
fix: sudo ln -f -s /opt/VBoxGuestAdditions-5.1.20/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vb ...
- Bootstrap——导航居中
这是采用了栅格,设置缩进,使看起来居中,但是手机浏览会靠到最左边.另外center-block类好像也不管用. <div class="row"> <ul cla ...
- layer的用法
layer的用法: 使用之前必须导入包如下: #import<QuartzCore/QuartzCore.h> 否则使用控件.layer时,是不会提示的,粘贴过来的代码会直接报错. 就拿v ...