LeetCode 第 292 题 (Nim Game)
2016-07-05 13:58
483 查看
LeetCode 第 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.
若干块石头,两个人交替取走一些,每次取只能取 1 到 3 颗。取到最后一块石头的人输。那么最优的策略是取走一些石头后剩下的石头是 4 的倍数。根据这个原则,这个题目就非常简单了。
bool canWinNim(int n) { return (n % 4 != 0); }
相关文章推荐
- LeetCode 第 292 题 (Nim Game)
- 电子烟使用评测
- mysql mysql.user 数据库表
- unity htcvive sdk 按键
- 2016春季练习——DP一水
- Mac OSX python多版本管理工具:pyenv 和 virtualenv搭建
- android 基础知识回顾--- “沙箱”机制sharedUserId跟签名
- 如何给变量取个简短且无歧义的名字
- AVLTree的实现
- windows下react-native android环境搭建注意事项
- 四个下拉框联动 省市区法院选择 js + ajax
- 前端性能优化:字符串的连接操作改进
- windows golang开发环境搭建--LiteIDE-最适合Go的IDE
- 0138 redis脚本实现分布式锁
- Matlab中hold on与hold off的用法
- 使用ShadowCopy+QuarksPwDump0.3a导出当前域内所有用户的hash
- sdut 3326 顺序表应用3:元素位置互换之移位算法
- java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionFactory
- hdwiki 在IIS 下的伪静态
- sql server创建视图操作