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 ...
随机推荐
- CI框架 -- 自动加载资源
CodeIgniter 的"自动加载"特性可以允许系统每次运行时自动初始化类库.辅助函数和模型. 如果你需要在整个应用程序中全局使用某些资源,为方便起见可以考虑自动加载它们. 支持 ...
- android中清空一个表---类似truncate table 表名 这样的功能 android sqlite 清空数据库的某个表
public void clearFeedTable(){ String sql = "DELETE FROM " + FEED_TABLE_NAME +";" ...
- Python——eventlet.websocket
使用该模块可以方便地创建 websocket 服务器,要创建一个websocket服务器,只需要将一个句柄函数用装饰器 WebSocketWSGI 装饰即可,然后这个函数就可以当做一个WSGI应用: ...
- c#中//注释和///注释的区别 智能注释 显示换行
///会被编译,//不会所以使用///会减慢编译的速度(但不会影响执行速度)///会在其它的人调用你的代码时提供智能感知 也是一种注释,但是这种注释主要有两种作用:1.这些注释能够生成一个XML文件. ...
- PHP和Apache结合 Apache默认虚拟主机
- Thinkphp5模板继承
代码 application\index\controller\index.php <?php namespace app\index\controller; use app\index\con ...
- PS1-4
export PS2="continue->" cat ps4.sh export PS4='$0.$LINENO+ ' set -x echo "PS4 demo ...
- 性能监控-TP理解
首先给出Google到的答案: The tp90 is a minimum time under which 90% of requests have been served. tp90 = top ...
- JS中 try...catch...finally (转)
JS的try..catch..finally var array = null; try { document.write(array[0]); } catch(err) { document.wri ...
- windows server 2003R2\2008R2\2012\2016 安装【故障转移群集】cluster
温故而知新! 靠,突然觉得it技能只要一年的时间就能忘记! virtualbox虚拟机安装群集,注意点: clone的虚拟机,sid问题.sysprep太慢了,不如重新安装快! 虚拟机增强组件,会影响 ...