LeetCode之路:292. Nim Game
来源:互联网 发布:javashop b2b2c 源码 编辑:程序博客网 时间:2024/06/10 21:10
一、引言
这是一道非常有趣的题目,有着非常简单的规则,却让我一直没有思考出来,甚至在思考这个问题的同时,还引申到了另一道题上面:台阶问题:斐波那契数列的扩展问题研究,有兴趣了解下的同学可以看看这里。
关于这道题,其实这是一个非常出名的游戏,有着不同的版本,LeetCode上面的这个题目是简化的版本。
这里直接把原题目粘贴过来:
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.
那么,这道题怎么做呢?
二、让我们来一个一个例子分析下这个情景
首先,我们来一个一个举例,举例的过程中,我们要记住,每人最多拿 3 个石头,最少必须拿 1 个石头:
看上面这个表,我们能看到这么一个规律:
当先手玩家遇到了非4的倍数,可以确保必胜;当先手玩家遇到了4的倍数的时候,后手玩家拿走指定数量的石头数量造成剩余4的倍数的石头数量后,后手玩家必胜。
规律总结出来了,真的非常简单,以至于代码更加简单:
class Solution { bool canWinNim(int n) { if (n % 4 == 0) return false; else return true; }}
哈哈哈,看到了更简单的方法:
class Solution { bool canWinNim(int n) { return n % 4; }}
真是好气啊~~~我思考了这么久都没想出来的问题呢,一行代码解决!
三、总结
其实我一看到这道题,脑海里第一个念头是,怎么跟台阶问题那么像,比如:
有一只青蛙,它一次可以跳 1 阶或者 2 阶台阶,那么请问它跳上 N 阶台阶有多少种走法?
这个问题呢,其实是探讨的石头的拿法,但是这道题并不需要拿法有多少,只需要总结规律是否必胜就行了。
总之,学会分析问题的方法。
To be stronger!
- Leetcode之路 292. Nim Game
- LeetCode之路:292. Nim Game
- LeetCode之Nim Game
- LeetCode之 Nim Game
- Leetcode之Nim Game
- leetcode 之Nim Game
- LeetCode之Nim Game
- [leetcode] 292.Nim Game
- 【LeetCode】292.Nim Game
- leetcode:292. Nim Game
- leetCode 292. Nim Game
- 292. Nim Game LeetCode
- LeetCode 292. Nim Game
- LeetCode 292. Nim Game
- LeetCode 292.Nim Game
- LeetCode 292. Nim Game
- [LeetCode]292.Nim Game
- <LeetCode> 292. Nim Game
- FragmentPagerAdapter中Fragment不能刷新问题
- jdk5.0的一些新特性(1)
- 第二天作业笔记
- POJ 2385 线性DP
- 远程DMA与本地DMA
- LeetCode之路:292. Nim Game
- CSS设置三角形
- 数论 扩展欧几里得用法详解
- Windows下Django+pycharm开发环境搭建
- 被坑水题——Kill boss
- maven引入jstl的jar包形式
- JNI的某些数组和字符串类型转换(转)
- 何时使用 “宏”
- 解决IE6,7,8JSON.stringify JSON 未定义问题