题目要求

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. JUnit+Mockito结合测试Spring MVC Controller

    [本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...

  2. HeroKu PaaS模式

    从HeroKu的官网看到一副流程图,演示了其提倡的应用构建通道,让包括个人开发者.创业团队.乃至各种规模的业务都能以自己的方式使用它,剩下的就是交给用户去开发出优秀的应用. 在开发者和应用的用户之间, ...

  3. 点云PCL中小细节

    计算点与点之间的距离的平局距离 double computeCloudResolution (const pcl::PointCloud<PointType>::ConstPtr & ...

  4. Java知多少(49)throw:异常的抛出

    到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下:    throw ThrowableInstance;这里,Thr ...

  5. RedHat Linux7.3 使用yum安装时报错,改用CentOS yum源

    在使用redhat linux的yum来安装或者更新软件的时候会有如下提示: [root@node1 home]# yum install package_name Loaded plugins: p ...

  6. 基于Java的数据采集(二)

    在上一篇文章<基于Java的数据采集(一)>:http://www.cnblogs.com/lichenwei/p/3904715.html 提到了如何如何读取网页源代码,并通过group ...

  7. 2 salt-masterless架构

    minion无master的使用用法 需要更改minion配置文件的三个配置项 [root@linux-node2 ~]# vim /etc/salt/minion file_client: loca ...

  8. ABBYY FineReader Pro for Mac有哪些特性(下)

    使用ABBYY FineReader Pro for Mac轻松转换纸质文档.PDF文件和数字文本照片为可编辑和可搜索的文件,再也不需要手动重新输入或格式化了,相反,可以编辑.搜索.共享.归档和复制文 ...

  9. [Hinton] Neural Networks for Machine Learning - Hopfield Nets and Boltzmann Machine

    Lecture 11 — Hopfield Nets Lecture 12 — Boltzmann machine learning Ref: 能量模型(EBM).限制波尔兹曼机(RBM) 高大上的模 ...

  10. 大杂烩 -- ArrayList的动态增长 源码分析

    基础大杂烩 -- 目录 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 问题:当ArrayList中放入的元素一直增加会如 ...