Leetcode Everyday: 292. Nim Game
2016-05-11 22:48
495 查看
https://leetcode.com/problems/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.
boolean
= !(boolean [n-1] && boolean [n-2] && boolean [n-3]);
T T T F T T T F T T T F .......
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) { int result = (n-1)%4; if(result == 3) return false; return true; } }
boolean
= !(boolean [n-1] && boolean [n-2] && boolean [n-3]);
T T T F T T T F T T T F .......
相关文章推荐
- PAT 1021
- 【C语言】从文件每次读入一行字符串,并把这些字符串添加到字符串数组中
- Android 在网上查看图片(最简单的版本)
- 关于badgeview使用中的问题
- python基础(内置函数+文件操作+lambda)
- 空指针和野指针
- 谈谈Android中的OOM
- 布隆过滤器
- iOS键盘弹出 视图向上滚动键盘高度
- OpenGL ES绘制3D纹理贴图
- 【AKOJ】1032-水仙花数
- android高级UI—ExpandableListview
- uboot启动流程
- CSS基础知识一(CSS的应用方式、CSS选择器、选择器的声明、文字CSS效果、)
- CSS基础知识二(段落设置CSS效果、图片边框、div和span区别、CSS控制背景、CSS控制链接)
- SCTF 2015 pwn试题分析
- WinForm设置控件焦点focus
- Alluxio1.0.1最新版(Tachyon为其前身)介绍,+HDFS分布式环境搭建
- 软件工程_东师站_第十周作业
- requests模块--python发送http请求