题目要求

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.

题目分析及思路

给定石头的数量,要求确定‘我’是否能赢得比赛。比赛要求是‘我’和同伴每人轮流从一堆石头中取走1-3块,取走最后一块石头的是赢家。‘我’可以先取,且‘我’和同伴都有最优的策略。该比赛的关键在4,只要给对方剩下4块石头,这样对方就输了。所以只要确定石头数是否是4的倍数,又因为是‘我’先拿,综上只要石头数不是4的倍数,‘我’就是赢家。

python代码

class Solution:

def canWinNim(self, n: int) -> bool:

return n % 4 != 0

LeetCode 292 Nim Game 解题报告的更多相关文章

  1. 【LeetCode】292. Nim Game 解题报告(Java & Python)

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

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

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

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

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Asp.Net 简繁转换

    帮助类 /// <summary> /// 中文字符工具类 /// </summary> public static class ChineseStringUtility { ...

  2. 【6集iCore3_ADP触摸屏驱动讲解视频】6-6 底层驱动之触摸操作

    源视频包下载地址:  链接:http://pan.baidu.com/s/1skQlWAT 密码:ymn7   银杏科技优酷视频发布区: http://i.youku.com/gingko8  

  3. AtomicInteger 源码阅读

    Package java.util.concurrent.atomic 这是一个小工具包,它的实际作用是提供了很多个无阻塞的线程安全的变量操作工具. 无阻塞的线程安全:其含义就是不使用 synchro ...

  4. 瀑布 敏捷 精益 devops

    敏捷:  分工角色  大项目分小项目   每个节点时间设置里程碑 Scrum实施的核心可以概括为“化繁为简”,从几个维度解释下: 团队角色的定义,将团队人员定义为三个角色,Scrum Master(主 ...

  5. React Native常用组件之ScrollView

    1. 两个要点 1.1 ScrollView必须有一个确定的高度才能正常工作 它实际上所做的就是将一系列不确定高度的子组件装进一个确定高度的容器(通过滚动操作) 通常有两种做法: 第一种: 直接给该S ...

  6. 超简单Windows安装Scrapy (仅需一步)

    网上很多关于windows安装Scrapy的教程都非常的繁琐,请看我给大家分享的教程,非常简单 一步完成. 超简单的安装方法: 下载地址: https://www.continuum.io/downl ...

  7. openwrt 域名ping不通 修改dns

    问题 wget: bad address 'downloads.openwrt.org' wget: bad address 'downloads.openwrt.org' wget: bad add ...

  8. iOS开发-- 一个苹果证书如何多次使用

    苹果的开发者账号限制开发者证书只能有5个,我们开发过程中遇到超过5个人需要真机调试的情况,如何解决这个问题呢? 有两种方式可以解决问题: 1. Revoke原来的证书----不推荐 将以前的证书“re ...

  9. mysql 冷热备份

    文章转自:http://www.linuxidc.com/Linux/2014-03/98710.htm 冷备份(OFF, 慢, 时间点上恢复)冷备份发生在数据库已经正常关闭的情况下,当正常关闭时会提 ...

  10. image-set实现Retina屏幕下图片显示详细介绍

    支持image-set:如果你的浏览器支持image-sete,而且是普通显屏下,此时浏览器会选择image-set中的@1x背景图像: Retina屏幕下的image-set:如果你的浏览器支持im ...