LeetCode (262):Nim Game
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:
- 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的更多相关文章
- 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
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- 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 ...
- Java [Leetcode 292]Nim Game
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
- 【算法功底】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 解题报告
题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...
- LeetCode 292 Nim Game(Nim游戏)
翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...
- [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 ...
- LeetCode: 292 Nim Game(easy)
题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...
随机推荐
- msvcp71.dll 怎么丢失的?如何修复
解决方法:另一台电脑上下载这个dll,再用优盘拷回来,复制到c:\windows\system32\下. 个人遇到的情况:迅雷下载东西时,或者在操作迅雷时出现的. win7 64位下 点击下载
- 从0到1实现SourceTree连接Gitlab
见下面的链接 http://note.youdao.com/noteshare?id=3622d02a38464c524222ede1b4fb06d2 SourceTree下载地址:Windows V ...
- hibernate的二级缓存----collection和query的二级缓存
collection二级缓存: 不使用集合的二级缓存时: 运行下面的代码: @Test public void testCollectionSecondLevelCache1(){ Departmen ...
- HDU3658—How many words
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3658 题目意思: 题目的意思是在52个英文字母里面选择m个字母组成一个字符串,满足以下两个条件.第一是 ...
- 如何让ios启动画面停留更长时间
几种方法: 方法1:在AppDelegate.m里写上 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithO ...
- 该死的Kafka,远程连接Kafka超时以及解决办法
关于消息的发布与订阅,之前一直使用的是activeMQ基于JMS的消息队列进行操作的,最近听说有一个更高效的消息的发布与订阅技术,就是Kafka. 关于kafka的介绍,在这里就不做过多讲解了,因为我 ...
- 【转】UML中类与类之间的5种关系表示
一.继承关系 继承指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力.在Java中继承关系通过关键字extends明确标识,在设计时一 ...
- QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp->removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)
最近项目需要软件具有中英文双语切换功能,而QT又自带此功能,现将实现方式记录下来. 说到中英文切换,少不了要了解QT的内部编码方式.在此就不详述QT编码方式了,具体可参考 彻底弄懂Qt的编码.只需要记 ...
- TP自适应
最近又要求职了,梳理了下这两年折腾的东西,发现有个产品很可惜,都开发完了,但是没上市.中兴的一款手表,我很喜欢那个金属壳子,结实,拿在手里沉甸甸,可以用来砸核桃. 当时调TP的时候,换了几个厂家,程序 ...
- php about session store db or cache
PHP关于Session的配置: 在php.ini中配置为:session.name = PHPSESSID 在请求开始的时候,会话名称会被重置并存储到session.name配置项. 所以要想在不改 ...