题目要求

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. Django基础学习之Cookie 和 Sessions 应用

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  2. 【GMT43智能液晶模块】例程五:IWDG看门狗实验——复位ARM

    实验原理: STM32内部包含独立看门狗,通过看门狗可以监控程序运行,程序运行 错误时,未在规定时间喂狗,自动复位ARM.本实验通过UI界面中按钮按下 停止喂狗,制造程序运行错误,从而产生复位. 示例 ...

  3. Gson - 学习

    Google 的 Gson 库,Gson 是一个非常强大的库,可以将 JSON 格式的数据转化成 Java 对象,也支持将 Java 对象转成 JSON 数据格式. Gson 依赖 本文将会快速开始使 ...

  4. Linux部署Web应用程序超链接下载中文名称文件404问题解决办法

    Web应用程序目录下有帮助文档,是中文名称的Word文件 超链接内容如下: <a href="jsp/plugin/用户手册.doc">用户手册</a> 开 ...

  5. RDP 数据库简介

    在扩增子数据分析中,有时会发现多个OTU 注释到了同一个species ,  为什么会出现这种情况呢? 首先既然在OTU水平能分开,说明序列的相似度小于97%,  同一个物种的同一个基因的片段相似度会 ...

  6. CentOS最小化安装(一)

    一.配置网络 切记记得配置DNS,否则Ping不通 在目录中进行网络配置文件的查找:  /etc/sysconfig/network-scripts/ 1   1 /etc/sysconfig/net ...

  7. linux memcached

    依赖库 yum install libevent libevent-deve 云安装 yum install memcached 源代码安装 wget http://memcached.org/lat ...

  8. pixmap和label设置图片自适应大小

    在label中添加pixmap来显示图片时,当图片过大时图片显示不全. 1.这时可以使用pixmap的scared()方法,来设置图片缩放. QPixmap QPixmap.scaled (self, ...

  9. php文件缓存方法总结

    为大家分享很全的php文件缓存,供大家参考,具体内容如下 <?php class cache {  private static $_instance = null;    protected ...

  10. SpringBoot(十四)-- 整合Swagger2

    1.pom依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...