作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/nim-game/

Total Accepted: 66290 Total Submissions: 125590 Difficulty: Easy

题目描述

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.

Example:

Input: 4
Output: false
Explanation: 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-n,n为当次我拿到子的个数。

Java代码:

public class Solution {
public boolean canWinNim(int n) {
return n % 4!= 0;
}
}

二刷,python.

转眼两年办过去了,再次做了这个题。python代码如下:

class Solution(object):
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
return n % 4 != 0

日期

2016/4/29 21:39:23
2018 年 11 月 9 日 —— 睡眠可以

【LeetCode】292. Nim Game 解题报告(Java & Python)的更多相关文章

  1. LeetCode 292 Nim Game 解题报告

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

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  4. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  5. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  6. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  7. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  9. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

随机推荐

  1. python函数理解 json.dump()

    信息来自python说明文档(https://docs.python.org/3/library/json.html) 函数功能 输出一个python对象到文件 函数声明 json.dump(obj, ...

  2. [R] 保存pheatmap图片对象到文件

    一般我们使用pheatmap通过Rstudio交互得到的图片在plots的Export导出即可,如何保存对象到文件呢?这个需求在自动化流程中很常见,作者似乎也没说明. 生成示例数据: test = m ...

  3. Docker Nginx-Proxy 容器Nginx Proxy反向代理

    Docker Nginx-Proxy 容器Nginx Proxy反向代理   简单介绍 Docker容器的自动Nginx反向代理   dockerhub地址 https://hub.docker.co ...

  4. 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  5. 运算符重载+日期类Date

    Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...

  6. A Child's History of England.36

    CHAPTER 11 ENGLAND UNDER MATILDA AND STEPHEN The King was no sooner dead than all the plans and sche ...

  7. tomcat在eclipse上发布,Perference下的server找不到解决办法

    help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...

  8. webservice--cxf和spring结合

    服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...

  9. HDFS初探之旅(一)

    1.HDFS简介                                                                                            ...

  10. 创建线程的第二种方式------实现Runnable接口的方式

    package cn.itcast.demo16.Demo07.Runnable;/** * @author newcityman * @date 2019/7/22 - 23:17 */public ...