题目要求

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. [转]css实现左侧宽度自适应,右侧固定宽度

    原文地址:https://segmentfault.com/a/1190000008411418 页面布局中经常用会遇到左侧宽度自适应,右侧固定宽度,或者左侧宽度固定,右侧自适应.总之就是一边固定宽度 ...

  2. 【Unity】不能新建项目

    问题:Unity5.5.2f1今天遇到个Bug,在启动器点击新建项目没有反应. 办法:先点击新建项目(没有反应),再点击Sign Out退出登录,然后再登录进来,就能跳到新建项目页面.

  3. InstallShield:卸载时文字叠加,文字乱码

    问题: InstallShield2010打包的程序.如果程序正在运行,卸载程序,提示关闭程序,然后消息界面出现文字叠加. 定位问题: 新建Installshield项目,依次修改SetupType, ...

  4. haproxy配置详解

    先看一个ha的配置文件: # # Global settings # global # to have these messages end up in /var/log/haproxy.log yo ...

  5. python初级 2 字符串格式化

    一.回顾 上次我们讲解的数据类型,有int,float,str,bool,NoneType五种 前两种和后两种都比较简单,str较为复杂 二.字符串格式化的使用场景: 有一种字符串, xxx 的内容都 ...

  6. Docker孵化的5个开源项目

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/81977243 回想过去短短 ...

  7. [React] 16 - Topic: Mixins & Higher-Order Components

    到底是个什么东东,待学习中... Ref: https://www.cnblogs.com/wonyun/p/5930333.html

  8. 终端IO(上)

    一.综述 终端IO有两种不同的工作方式: 规范方式输入处理.在这种方式中,终端输入以行为单位进行处理.对于每个读要求,终端驱动程序最多返回一行. 非规范方式输入处理.输入字符不以行为单位进行装配 如果 ...

  9. [IOI 2000]POJ 1160 Post Office

    Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...

  10. VS Code非英语版本连接TFS错误解决方案

    使用VS Code连接TFS时,提示以下错误: (team) It appears you have configured a non-English version of the TF execut ...