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.

对于这个问题,我的第一反应是用递归来解决。递归容易理解,而且代码很简单,如下:

 public class Solution {
public boolean canWinNim(int n) {
if(n<=0) return false;
if(n==1 || n==2 || n==3) return true; if(!canWinNim(n-1)) return true;//如果第一步移除一个,对方是否能赢
if(!canWinNim(n-2)) return true;//如果第一步移除两个,对方是否能赢
if(!canWinNim(n-3)) return true;//如果第一步移除三个,对方是否能赢 return false;
}
}

但是递归的时间复杂度很高,那么如何对这个问题进行改进呢?

看了下别人的答案,如下:

 public class Solution {
public boolean canWinNim(int n) { if(n%4 == 0) return false;
else return true;
}
}

这是为什么呢?n如果能被4整除则不能赢,如果不能整除则能赢,这是为什么呢?

当N=1、2、3时,那么肯定能赢。如果为4时,肯定不会赢。如果N=5、6、7时,先手可以通过取1颗,2颗或者3颗,把问题成:有4颗石头,让对方先手,那么对方肯定输。如果N=8时,无论先手第一次取几颗,对方都有办法把问题变成N=4的规模,那么肯定输。同样,N为9、10、11肯定赢,为12时肯定输。归纳发现:当N为4的倍数时肯定输,不为4的倍数时肯定赢。所以我们就会看到上面的代码。

启发:有些问题的解决不一定要依赖算法,其实找到问题内在的规律解决起来会更迅速简单方便。

LeetCode OJ 292.Nim Game的更多相关文章

  1. LeetCode OJ 292.Nim Gam148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种 ...

  2. LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. 【Leetcode】292. Nim Game

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

  4. 【一天一道LeetCode】#292. Nim Game

    一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...

  5. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 力扣(LeetCode)292. Nim游戏 巴什博奕

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

  7. 【Leetcode】292. Nim游戏

    题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1  ...

  8. LeetCode OJ:Nim Game(Nim游戏)

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

  9. Leetcode题目292.Nim游戏(脑筋急转弯)

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

随机推荐

  1. 使用OllyDbg从零开始Cracking CHM

    需要的小伙伴拿走,百度云链接:https://pan.baidu.com/s/1pLJa5dh.另有一些视频资料,太多,需要的可以留言.

  2. redis 2

    http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage 在Ubuntu下安装reids redis-2. ...

  3. try{}catch{}finally{}的手记

    try{ System.out.println("执行try"); int = 6 / 0; return 1; }catch(Exception e){ System.out.p ...

  4. Linux 相关的error处理

    1  dpkg: error: duplicate file trigger interest for filename Notice the first and last lines of /var ...

  5. 理解委托(delegate)及为什么要使用委托

    理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...

  6. C# 泛型 Func<object, string, bool> filter

    Func<object, string, bool>是泛型,你可以先把他看成一个普通类型,比如stringpublic class Func{ } // 自定义个普通类. Func fil ...

  7. hdu1011

    /*比较苦逼的树形DP,慢慢来吧!不着急*/#include <iostream>#include <vector>using namespace std;const int ...

  8. css ::before和::after伪元素的用法

    css ::before和::after伪元素的用法:http://blog.dimpurr.com/css-before-after/

  9. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  10. Digital Ocean VS. Linode对比评测

    美国攻城师Zach Schneider是linode vps资深用户,他最近却转向了Digital Ocean,原因是什么呢?来看这篇digitalocean linode对比评测的文章: 用了两年的 ...