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?

思路:

当heap中有1、2、3个stones时一定获胜,当有4个stones时一定会输。那么当有5个stones的时候,我们可以先拿出来一个stones,此时相当于我们面对4个stones时的情形。

依次可以类推出,当heap中恰好有4N(N=1,2,3......)个stones时,我方一定会输。

java代码:

public class Solution {
public boolean canWinNim(int n) {
if(n%4==0) return false;
else return true;
}
}

LeetCode (262):Nim Game的更多相关文章

  1. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  3. 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 ...

  4. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  5. 【算法功底】LeetCode 292 Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. LeetCode 292 Nim Game 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  7. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  8. [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 ...

  9. LeetCode: 292 Nim Game(easy)

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

随机推荐

  1. Ubuntu 16.04 安装 Gnome 桌面环境

    个人博客链接:Ubuntu 16.04 安装 Gnome 桌面环境

  2. freenas 11.2踩过的坑

    修改SMB最小协议 服务器最小协议由FreeNAS上的sysctl控制. 在System-> Tunables 下添加sysctl来使其永久化:Variable = freenas.servic ...

  3. ubuntu重启不清除 /tmp 设置

    gedit /etc/default/rcS, 把TMPTIME=0 修改成 TMPTIME=-1,保存退出即可.

  4. Struts2+Spring3+MyBatis3整合以及Spring注解开发

     分类: Web(2)  版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做一个SpringMVC+spring+MyBatis的项目,突然想起以前自己要搭建一个Struts2+Sprin ...

  5. centos 7.5 最小化安装

    参考:https://www.tecmint.com/centos-7-installation/ ================================================== ...

  6. php+nginx上传文件配置

  7. requests设置Authorization

    headers = {"Authorization", "Bearer {}".format(token_string)} r = requests.get(& ...

  8. Java实现断点续传

    原理: 断点续传的关键是断点,所以在制定传输协议的时候要设计好,如上图,我自定义了一个交互协议,每次下载请求都会带上下载的起始点,这样就可以支持从断点下载了,其实HTTP里的断点续传也是这个原理,在H ...

  9. arcgis server/portal 日志格式化脚本

    友好化格式阅读 背景 通过arcgis for server manager中的logs选项卡可以查看当前站点的日志.其该页面提供了友好的日志显示方式. 但是在实际情况中,如arcgis server ...

  10. node.js + mongodb 做项目的详解(二)

    这次内容是结合bootstrap把登陆注册做好,还有就是express的中间件等问题. 看这篇博客之前建议先看我上篇写的那篇博客http://www.cnblogs.com/hubwiz/p/4118 ...