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. hibernate java.sql.SQLException

    异常:java.sql.SQLException oracle.net.ns.NetException java.net.ConnectException 提示:The Network Adapter ...

  2. appium使用真机做安卓移动端自动化测试

    1.PC往手机上安装apk文件:adb install apk文件完整路径 2.获取app包名和activity的命令:使用aapt;aapt是sdk自带的一个工具,在sdk\builds-tools ...

  3. this的相关知识

    一如既往,直接上代码: function fn(name,age){ var obj=new Object(); obj.name=name; obj.age=age; obj.talk=functi ...

  4. ACM常用模板

    数论: 中国剩余定理(互质与非互质通用版) ],r[]; int e_gcd(int a,int b,int &x,int &y) { ) { x=; y=; return a; } ...

  5. ES 集群调整、升级 最佳实践

    日常应用中我们会经常对es 集群做一些参数调整或者升级版本,但是每次关闭节点再打开 其中的数据同步的痛苦估计有很多人领悟过(有可能出现IO或者网络拥堵导致恶性循环)官网有套方案可以尝试一下: 1.关掉 ...

  6. Cmake常用指令

    1. cmake_minimum_required(VERSION 2.8)           检查cmake的版本,至少为2.8 2. project(helloworld)           ...

  7. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  8. 利用yield关键字输出杨辉三角

    最近学习了下python,发现里面也有yield的用法,本来对C#里的yield不甚了解,但是通过学习python,对于C#的yield理解更深了!! 不多说了,小学生水平的表达能力伤不起.... 直 ...

  9. hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路

    BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...

  10. MC-设置 止盈

    using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...