lleetcode 292. 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.
本题中,由于你是第一个拿石子的人,所以,只要所有的石子数目为4的整数倍,那么就必然会输,因为你的对手可以保证在你拿走一次石子后,始终每次拿走石子的个数和你拿走石子的个数和为4。
class Solution {
public:
bool canWinNim(int n) {
if(n % 4 ==0)
return false;
else
return true;
}
};
lleetcode 292. Nim Game的更多相关文章
- 292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...
- 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 ...
- 292. Nim Game(C++)
292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...
- 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
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 292. Nim游戏
292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...
- 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 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- 292. Nim Game - LeetCode
Question 292. Nim Game Solution 思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢. n 是否能赢 1 true 2 true 3 true 4 false 不 ...
随机推荐
- BZOJ2164 : 采矿
树链剖分+线段树,每个节点维护以下信息: (1)单独在某个点分配$i$个人的最大收益.可以$O(m)$合并. (2)分配$i$个人的最大收益.可以用$O(m^2)$合并. 时间复杂度$O(c(m^2\ ...
- [转]error while loading shared libraries 错误解决办法总结
http://blog.csdn.net/wallwind/article/details/7580659 错误信息: error while loading shared libraries: li ...
- UVa 11922 & splay的合并与分裂
题意: 1个1—n的排列,实现一下操作:将a—b翻转并移动至序列的最后. SOL: splay维护区间的裸题——不过平衡树的题目貌似都是裸的吧...就是看操作的复杂程度罢... 如何取区间呢,我们在s ...
- CentOS 6.6 下配置软RAID5
在虚拟机中增加4块5G的硬盘 安装依赖包 yum install -y parted mdadm 查看一下硬盘信息fdisk -l
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- 洛谷 P1372 又是毕业季I Label:None
题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...
- 【转载】LoadRunner11下载以及详细破解说明
前期准备:LoadRunner11 下载请猛戳这里 传送门LoadRunner破解文件 下载请猛戳这里 传送门LoadRunner注册表清理工具 下载请猛戳这里 传送门 LoadRunner11破解方 ...
- [知识点]Tarjan算法
// 此博文为迁移而来,写于2015年4月14日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxnx.html UPD ...
- [深入浅出Windows 10]QuickCharts图表控件库解析
13.4 QuickCharts图表控件库解析 QuickCharts图表控件是Amcharts公司提供的一个开源的图表控件库,这个控件库支持WPF.Silverlight.和Windows等 ...
- 【HDU】1814 Peaceful Commission
http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选 ...