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. DNS服务架设 redhat linux

    安装dns服务和管理工具 yum install bind   bind-chroot bind-utils -y 服务名为named 在没有启动服务之前服务的主配置文件在/etc/named.con ...

  2. Linux修改SSH端口和禁止Root远程登陆

    Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...

  3. Scala学习---数组

    1.编写一段代码,将a设置为一个n个随机整数的数组,要求随机数介于0(包含)和n(不包含)之间 /** * Created by vito on 2017/1/11. */ object ex1 { ...

  4. Namenode写Journalnode超时,导致Namenode挂掉的问题

    昨天还好好的集群,今天早上来看又挂掉了,还好是家里的测试服务器集群... 首先,查看了Namenode的状态,发现两台Namenode只剩下一台了,赶紧到挂了的那台去查看了logs下的日志: -- : ...

  5. Major and minor numbers

    The major nuber is the driver associated with the device, while the minor number is used by the kern ...

  6. ios根据文本自适应 然后 搭建类似如下效果

    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tbSecond.size.width, 0)]; hea ...

  7. C#的面向对象

    一.面向对象编程(OOP)是将现实中的事物抽象花,其设计的重点就是类的设计. 二.类是面向对象编程的设计核心,实际上是一种复杂的数据类型.将不同类型的数据和与这些数据就相关的操作封装在一起,就构成类. ...

  8. post请求和get请求

    get请求在链接后面带参数,容易出现乱码,是坑(慎用),有固定的长度 一般的用的就是post方式 <form action="<%=basePath%>upload&quo ...

  9. hdu和poj的基础dp30道

    题目转自:https://crazyac.wordpress.com/dp%E4%B8%93%E8%BE%91/ 1.hdu 1864 最大报销额 唔,用网上的算法连自己的数据都没过,hdu的数据居然 ...

  10. POJ 2209 The King#贪心

    (- ̄▽ ̄)-*  水 //水题:潜力^e为正数(e为2时都可以)的儿子都可以去上战场了, //英文要看懂,exponent指数,不超过3的正数 #include<iostream> #i ...