LeetCode 292. Nim Game
Problem:
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 to 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 stones in the heap, then you will never win the game: no matter , , or stones you remove, the last stone will always be removed by your friend.
此题为技巧题,需知道Nim Game的Wining Strange,即在自己的局中,如果剩余数目为4的倍数,则不可能取胜。因此检测N是否为4的倍数即可。Background参见http://www.cdf.toronto.edu/~ajr/270/probsess/03/strategy.html
class Solution {
public:
bool canWinNim(int n) {
if(n% == ) return false;
else return true;
}
};
LeetCode 292. Nim Game的更多相关文章
- 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 ...
- 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个石子之后都是必胜,则当前必败 ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- LeetCode 292. Nim Game (取物游戏)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- 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 ...
随机推荐
- 如何添加商*通新对话快捷链接?不用js代码
我们在使用商务通一般都是在页面中嵌入一段js代码,如果您是js洁癖,是不是在想着如何直接用一张小图加上商*通新对话链接来代替呢?好,那就一起来研究一下吧. 首先,我们打开一个有商*通js弹窗的页面,比 ...
- Android插件化框架研究-DroidPlugin
直接贴上我做的ppt.
- Unity3D 原生Android结合UnityPlayerActivity开发遇到的问题
需求是原生Android 的Activity启动UnityPlayerActivity,按Back键后返回原来的Activity 1.在AndroidManifest.xml中的UnityPlayer ...
- MySql的一些用法
1.怎样找到MySql数据的存储目录? 答:从服务中查看正在运行的MySql,查看它的启动参数,可能是这个样子: "D:\Program Files\MySQL\MySQL Server 5 ...
- Demo中的IOC自定义实现
在做练习的时候,小小项目,使用IOC控件觉得麻烦,使用工厂觉得不高大上啊,自己写个简陋的依赖注入IOC吧; 控制反转(IOC)是管理映射依赖的的,是依赖倒置(DIP)的实现方式; 依赖倒置(DIP)是 ...
- 使用Visual Leak Detector for Visual C++ 捕捉内存泄露
什么是内存泄漏? 内存泄漏(memory leak),指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段 ...
- gulp学习-gulpfile
安装gulp 假设已经安装了node 和npm (淘宝的cnpm很适合国内使用). 1.首页全局安装gulp. 1 npm install --global gulp 2.其次局部安装gulp.(注: ...
- 【jQuery】--图片轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Eclipse启动Tomcat时发生java.lang.IllegalArgumentException: <session-config> element is limited to 1 occurrence
在学习struts 2时,为了方便,直接从下载的struts的apps目录下的struts2-blank.war压缩包下的WEB-INF\复制的web.xml,当我启动Tomcat时,发生 java. ...
- MVC Code First 当实体类发生变化时,如何自动更新数据库表
下面做一个例子,Category是用户新建的一个实体类,然后添加一个字段,然后让数据库中的Category表也添加一个字段 1.Category.cs