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: 赢
2: 赢
3: 赢
4: 不赢
5: 赢
6: 赢
7: 赢
8: 不赢
*/
class Solution {
public:
bool canWinNim(int n) {
if (n % == ){
return false;
}else{
return true;
}
}
};

好像很简单的样子。。

292. Nim Game(easy)的更多相关文章

  1. LeetCode: 292 Nim Game(easy)

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  2. 292. Nim Game

    292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...

  3. 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 ...

  4. 292. Nim Game(C++)

    292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...

  5. 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) { ...

  6. 【Leetcode】292. Nim Game

    problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...

  7. 292. Nim游戏

    292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...

  8. 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个石子之后都是必胜,则当前必败 ...

  9. Java实现 LeetCode 292 Nim游戏

    292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...

随机推荐

  1. JS引擎线程的执行过程的三个阶段(二)

    继续JS引擎线程的执行过程的三个阶段(一) 内容, 如下: 三. 执行阶段 1. 网页的线程 永远只有JS引擎线程在执行JS脚本程序,其他三个线程只负责将满足触发条件的处理函数推进事件队列,等待JS引 ...

  2. [Luogu4705] 玩游戏

    Description 给定两个长度分别为 \(n\) 和 \(m\) 的序列 \(a\) 和 \(b\).要从这两个序列中分别随机一个数,设为 \(a_x,b_y\),定义该次游戏的 \(k\) 次 ...

  3. [Code+#3] 寻找车位

    Description 给定一个大小为 \(n\times m\) 的 \(01\) 矩阵. 要求支持:单点翻转,询问子矩形内部最大正方形. \(n\times m\leq 4\cdot 10^6,n ...

  4. 利用BGP虚拟下一跳实现链路负载均衡

    最近针对BGP链路负载均衡方案“虚拟下一跳”进行了总结,现将总结的PPT贴上来.

  5. Redux,基础

    在学习了React之后, 紧跟着而来的就是Redux了~ 在系统性的学习一个东西的时候, 了解其背景.设计以及解决了什么问题都是非常必要的. 接下来记录的是, 我个人在学习Redux时的一些杂七杂八~ ...

  6. localhost和127.0.01 区别

    笔者最近调试程序时遇到的一个问题,localhost不能访问但127.0.0.1可以访问. 一.原理 我估计大多数人都不会去想localhost到底与127.0.0.1有什么不同,就比如我,有时候用h ...

  7. Redis过期策略

    一.设置过期时间 expire key time(以秒为单位) -- 这是最常用的方式 setex(String key, int seconds, String value) -- 字符串独有的方式 ...

  8. JS table内容转成二维数组,支持colspan和rowspan

    思路:1.先初始化colspan的数据到数组2.根据rowspan和colspan计算th和td的矩阵二次填充数组 说明:需要引用到第三方库jQuery,table中的th和td行和列跨度必须正确 & ...

  9. 关于ajax用户名验证和jquery实现简单表单验证

    首先来说用户名验证: 前台: <tr> <td class="tableleft">教师编号</td> <td><input ...

  10. DAS、SAN和NAS三种存储方式

    DAS存储 DAS存储在我们生活中是非常常见的,尤其是在中小企业应用中,DAS是最主要的应用模式,存储系统被直连到应用的服务器中,在中小企业中,许多的数据应用是必须安装在直连的DAS存储器上. DAS ...