LeetCode OJ 292.Nim Game
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的更多相关文章
- LeetCode OJ 292.Nim Gam148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种 ...
- 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 ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 【一天一道LeetCode】#292. Nim Game
一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...
- 【LeetCode】292. Nim Game 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 力扣(LeetCode)292. Nim游戏 巴什博奕
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...
- 【Leetcode】292. Nim游戏
题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 ...
- 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 ...
- Leetcode题目292.Nim游戏(脑筋急转弯)
题目描述: 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个 ...
随机推荐
- C#使用LitJson解析JSON(转)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decembe ...
- UIView 和 CALayer 的区别和联系
UIView是在/System/Library/Frameworks/UIKit.framework定义,也就是处于Cocoa Touch层. CALyer是在/System/Library/Fram ...
- 郑州尚学堂:链表的C语言如何实现动态内存分配
一.为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组.比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数 ...
- 以Jar形式为Web项目提供资源文件
以Jar形式为Web项目提供资源文件 http://www.webjars.org/ Web前端使用了越来越多的JS或CSS如jQuery, Backbone.js 和Twitter Bootstra ...
- jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部
jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部:http://www.haorooms.com/post/jquery_scroll_upanddown //滚动条滚动加载更多内容 //判 ...
- 【NOIP2007提高组】字符串展开
[题外话]这道题纯粹考验耐心,某些经常调程序调到摔键盘的人可以尝试 [题外话2]除了考耐心以外完全没有什么难点 [题外话3]也许会稍微恶心一点? [题外话4]其实我是在别人军训的时候滚来更博客的简直2 ...
- 原生态 php连接mysql
<?php$host = 'localhost';$user = 'root';$pass = '';$dbname = 'test';$con = mysql_connect($host,$u ...
- Hihocoder 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年 ...
- LeetCode OJ combine 3
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { retur ...
- 比较不熟的JavaScript点滴,慢慢前行,附带简单复杂化的php小计算器一份
interface.php <html> <head> <meta charset="utf-8" /> <title>这是一个简单 ...