LeetCode (262):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.
Hint:
- 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LeetCode: 292 Nim Game(easy)
题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...
随机推荐
- excel提取汉字拼音首字母
本文转载:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 利用Excel表格中的宏,轻松提取首字母 方法/步骤 1.启动E ...
- 170330、Spring中你不知道的注入方式
前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...
- Java 调用并执行cmd命令
cmd java 调用 执行 概要: Java 调用并执行cmd命令 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
- Spark源码分析 – Executor
ExecutorBackend 很简单的接口 package org.apache.spark.executor /** * A pluggable interface used by the Exe ...
- Centos locate 文件搜索命令(十一)
locate命令 locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命令所搜索的后台数据库 updatedb 更新数据库 locate搜索 ...
- Spring数据访问和事务
1.模型 2.解耦 3.实现 3.1 核心接口 3.2 代码分析 3.2.1 事务管理 3.2.2 数据访问 4.使用 4.1 编程模式 4.2 配置模式 4.2.1 声明式配置方式 4.2.2 注解 ...
- JUnit4.12 源码分析(二)之TestRule
1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...
- Androidstudio中导入内部依赖模块总结
今天刚从GitHub上找了一个不错的项目,想要把它导入自己的项目中,过程中也遇到了一些小问题,总结一下,以便复习回顾!!!! 1.首先将从GitHub上下载的压缩包进行解压,找到其中的项目文件,直接复 ...
- Java中的哈夫曼树
package com.ietree.basic.datastructure.tree; import java.util.ArrayDeque; import java.util.ArrayList ...
- #pragma 的用法
它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征.依据定义,编译指示是机 ...