【一天一道LeetCode】#292. Nim Game
一天一道LeetCode
从今天开始,调整规律,不按顺序做,从easy开始!
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
Hint:
1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
(二)解题
题目大意:nim游戏,游戏的规则是:桌上有一堆石头,数量为n,你和你的朋友每次从里面拿1~3个石头,谁最后一个拿完谁就赢了。
初次看到这题,真是一点思路都没有,不过,突然想起来在牛客网上刷过这题,【面试笔试算法】牛客网一站通Offer编程题2016.4.19
印象中是到找规律的题,于是开始一个一个找规律。
1--true
2--true
3--true
4--false
5--true
6--true
7--true
8--false
于是,这道题一句话就能解决了。哈哈,看到AC的那一刻,真是觉得,题目吓人,一大堆,讲的云里雾里,结果一句话就摆平了!!
class Solution {
public:
bool canWinNim(int n) {
return n%4;
}
};
【一天一道LeetCode】#292. Nim Game的更多相关文章
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- LeetCode 292. Nim Game (取物游戏)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- Java [Leetcode 292]Nim Game
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
- 【算法功底】LeetCode 292 Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode 292 Nim Game 解题报告
题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...
- LeetCode 292 Nim Game(Nim游戏)
翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...
- [LeetCode] 292. Nim Game_Easy tag: Math
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
随机推荐
- salt基本使用之二(2)
1.Event和Reactor Event是saltstack里面的对每个事件的一个记录,相比job更加底层. 一,查看Event事件 可以在master下再开一个终端执行以下命令可以查看Event事 ...
- C语言 分支与循环 递推思想 穷举 流程的转移控制
条件语句 开关控制语句(SWITCH语句) 象坐电梯一样,break是按的楼层,不加break则会一直执行下去. 上面程序有细节BUG,边界测试输入-5,105时由于整除会得到错误的结果. 解决方法: ...
- Redis出现多线程调用时抛出 [B cannot be cast to java.lang.Long] 异常
原因分析: 多个线程同时调用了同一个jedis对象,导致内存数据被多个线程竞争,产生数据混乱 (或者大家都用通一个redis获取同一个实例,登录同一个账号使用缓存时报错) 解决方案:每个线程都new出 ...
- 基于babylon3D模型研究3D骨骼动画(1)
3D骨骼动画是实现较为复杂3D场景的重要技术,Babylon.js引擎内置了对骨骼动画的支持,但Babylon.js使用的骨骼动画的模型多是从3DsMax.Blender等3D建模工具转换而来,骨骼动 ...
- 关于mysql安装到最后一步老是停留在starting server,显示无响应
从昨天晚上到今天安装MySQL花了好长的时间,一直是在后面starting server 这部就显示无响应,查资料了解到是MySQL有残留,有些注册表文件需要手动清理,下面是具体方法. 1.先用卸载软 ...
- python中修改字符串的几种方法
在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...
- Spring + mybatis 集成
具体项目可参照:https://github.com/LuoXiaoyi/springmvc 一.环境准备:Spring4.3.5 + Mybatis3.4.6 + Mybatis-Spring 1. ...
- URL重定向漏洞,python打造URL重定向漏洞检测脚本
前言: 今天学习了重定向漏洞,这个漏洞比较好理解 漏洞名:URL重定向漏洞 威胁:低 漏洞的来源:开发者对head头做好对应的过滤和限制 例子: 有漏洞的网站:http://a.com/x.php?u ...
- Mongo Index
摘要 mongo 的索引非常强大,和关系型数据库索引没什么区别.这里主要介绍本人在mongo索引上的犯的错. 索引种类 1.单字段索引 2.复合索引 多个字段索引 如{name:1,address:1 ...
- 20160208.CCPP体系详解(0018天)
程序片段(01):main.c 内容概要:PointWithOutInit #include <stdio.h> #include <stdlib.h> //01.野指针详解: ...