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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


 
 
class Solution {
public boolean canWinNim(int n) {
return n % != ;
}
}

Brainteaser-292. Nim Game的更多相关文章

  1. 292. Nim Game

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

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

  3. 292. Nim Game(C++)

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

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

  5. 【Leetcode】292. Nim Game

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

  6. 292. Nim游戏

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

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

  8. Java实现 LeetCode 292 Nim游戏

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

  9. 292. Nim Game - LeetCode

    Question 292. Nim Game Solution 思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢. n 是否能赢 1 true 2 true 3 true 4 false 不 ...

  10. LeetCode 292. Nim Game (取物游戏)

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

随机推荐

  1. linux下xampp(apache)中配置域名访问,以及遇到的问题

    xampp中apache使用域名访问 一.首先找到/opt/lampp/etc/httpd.conf: # Virtual hosts Include etc/extra/httpd-vhosts.c ...

  2. 什么是RNA-Seq (RNA Sequencing)

    什么是RNA-Seq (RNA Sequencing) 2011-07-14 ~ ADMIN 随着ome为词尾的各种组学的出现,转录组学已经成为了人们了解生物信息的一个重要组成部分.人们使用了许多办法 ...

  3. 抓取访客ip

    $realip = $_SERVER['REMOTE_ADDR'] $proxy_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);//有用到代理 ...

  4. python之数据类型3和文件操作

    一 字典属性方法补充 key是不变的类型,字典能快速查找,基于哈希索引 不可变类型 int  bool  str  tuple  可变类型:list  dict  set 1 clear  :清空 # ...

  5. 2018.10.14 bzoj4571: [Scoi2016]美味(主席树)

    传送门 自认为是一道思想很妙的题. 直接分析问题. 如果没有xxx的干扰直接上可持久化01trie01trie01trie走人. 但现在有了xxx这个偏移量. 相当于把整个01trie01trie01 ...

  6. Docker mysql启动自动按顺序导入sql

    1.目录结构 -rw-r--r-- root root Jan : Dockerfile -rw-r--r-- root root Jan : initdb.sh drwxr-xr-x root ro ...

  7. js正则表达式汇集

    1.只允许中文.字母.数字.中划线.下划线.空格.中文的().英文的()和#,只能以数字.中文.字母.下划线开头,长度为2至40之间 validateTemplateName: function(va ...

  8. MAC安装远程工具Securecrt的破解方式(详细有图)

    想要实现mac的远程连接功能,本来想使用终端的,但是终端的很多功能是欠佳的,所以决定安装一款,像windows的xshell一样好的软件,所以选择了这款Securecrt. 首先准备两个东西,一个是S ...

  9. Spring3.x错误---- Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

    Spring3.x错误: 解决方法: 缺少cglib的包, 下载地址: http://sourceforge.net/projects/cglib/files/latest/download?sour ...

  10. Java 窗体居中 通用代码

    Toolkit kit = Toolkit.getDefaultToolkit();    // 定义工具包    Dimension screenSize = kit.getScreenSize() ...