LeetCode 292 Nim Game 解题报告
题目要求
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 解题报告的更多相关文章
- 【LeetCode】292. Nim Game 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- Java compiler level does not match the version of the installed Java project facet解决办法
意思就是project facet 和 java compiler level 不一致 解决办法: 修改project facet 方法一: 选中工程,右键 Property -> Projec ...
- Ubuntu下的Wine&WineQQ
一.安装Wine 1.添加PPA sudo add-apt-repository ppa:ubuntu-wine/ppa 2.更新列表 sudo apt-get update 3.安装Wine sud ...
- hbase源码系列(十五)终结篇&Scan续集-->如何查询出来下一个KeyValue
这是这个系列的最后一篇了,实在没精力写了,本来还想写一下hbck的,这个东西很常用,当hbase的Meta表出现错误的时候,它能够帮助我们进行修复,无奈看到3000多行的代码时,退却了,原谅我这点自私 ...
- opencv_java import org.opencv.highgui.Highgui,类中无imread方法
opencv_java import org.opencv.highgui.Highgui,提示错误 2018年01月19日 14:50:25 小码农的路程 阅读数:358 原因:1.OpenCV ...
- Java知多少(58)线程Runnable接口和Thread类详解
大多数情况,通过实例化一个Thread对象来创建一个线程.Java定义了两种方式: 实现Runnable 接口: 可以继承Thread类. 下面的两小节依次介绍了每一种方式. 实现Runnable接口 ...
- Java知多少(60)isAlive()和join()的使用
如前所述,通常你希望主线程最后结束.在前面的例子中,这点是通过在main()中调用sleep()来实现的,经过足够长时间的延迟以确保所有子线程都先于主线程结束.然而,这不是一个令人满意的解决方法,它也 ...
- IDEA的这八条配置你一定要改!
一定要改的八条军规.快捷键使用idea默认的,与其他同事保存一致 自动编译开关 忽略大小写开关 智能导包开关 悬浮提示开关 取消单行显示tabs的操作 项目文件编码 滚轴修改字体大小 设置行号显示 在 ...
- Golang Gin 框架 Route备注
https://www.jianshu.com/p/d4b52187d233 https://blog.csdn.net/weixin_34210740/article/details/8602756 ...
- ElasticSearch6(三)-- Java API实现简单的增删改查
基于ElasticSearch6.2.4, Java API创建索引.查询.修改.删除,pom依赖和获取es连接 可查看此文章. package com.xsjt.learn; import java ...
- redis最大缓存和回收策略