【leetcode】1025. Divisor Game
题目如下:
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number
Non the chalkboard. On each player's turn, that player makes a move consisting of:
- Choosing any
xwith0 < x < NandN % x == 0.- Replacing the number
Non the chalkboard withN - x.Also, if a player cannot make a move, they lose the game.
Return
Trueif and only if Alice wins the game, assuming both players play optimally.Example 1:
Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.Example 2:
Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.Note:
1 <= N <= 1000
解题思路:假设当前操作是Bob选择,我们可以定义一个集合dic = {},里面存储的元素Bob必输的局面。例如当前N=1,那么Bob无法做任何移动,是必输的场面,记dic[1] = 1。那么对于Alice来说,在轮到自己操作的时候,只有选择一个x,使得N-x在这个必输的集合dic里面,这样就是必胜的策略。因此对于任意一个N,只要存在 N%x == 0 并且N-x in dic,那么这个N对于Alice来说就是必胜的。只要计算一遍1~1000所有的值,把必输的N存入dic中,最后判断Input是否在dic中即可得到结果。
代码如下:
class Solution(object):
dic = {1:1}
def init(self):
for i in range(2,1000+1):
flag = False
for j in range(1,i):
if i % j == 0 and i - j in self.dic:
flag = True
break
if flag == False:
self.dic[i] = 1 def divisorGame(self, N):
"""
:type N: int
:rtype: bool
"""
if len(self.dic) == 1:
self.init()
return N not in self.dic
【leetcode】1025. Divisor Game的更多相关文章
- 【LeetCode】1025. Divisor Game 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 动态规划 日期 题目地址:https://l ...
- 【Leetcode_easy】1025. Divisor Game
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- LintCode之在O(1)时间复杂度删除链表
题目描述: 分析:因为题目要求不能用循环,而且只给了要删除的节点,并没有给链表.所以我无法取得要删除节点的前一个节点,只能在待删除的节点以及下一个节点上做文章.我的思路是:将待删除的节点的下一个节点的 ...
- MySQL定义数据库对象之指定definer
mysql创建view.trigger.function.procedure.event时都会定义一个Definer: SQL SECURITY 有两个选项,一个为DEFINER,一个为INVOKER ...
- P2158仪仗队
今天早上你谷崩了 由于脑子抽筋,所以选了一道数学题来做.做着做着就疯了 传送 窝盟先画张图冷静冷静 这是样例的图,其中蓝点是有学生的地方. 窝盟来看一下那些学生可以被C君看到. 假设这张图是一个坐标系 ...
- centos R包 tidyverse安装
tidyverse安装失败,install.packages('tidyverse') 错误原因大概是其中有个依赖包xml2安装不上,解决办法是yum install libxml2-devel,这样 ...
- mysql一主多从配置详情
https://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html 1.准备好3台虚机,一台master,两台slave且都安装好mysql 2.主 ...
- [LeetCode] 136. Single Number(位操作)
传送门 Description Given an array of integers, every element appears twice except for one. Find that si ...
- Git001--简介
Git--简介 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00 ...
- 二叉树BinTree类定义
#include<iostream> using namespace std; template<class T> struct BinTreeNode{//二叉树结点类 T ...
- python学习第十八天计算机字符编码
人类语言和计算机语言二进制怎么沟通,最开始字符编码为ascii码对照表 包括数据和字母,没有汉字,中国自己搞了一套自己的编码 gb2312编码后来发展GBK编码,日本,韩国都,甚至台湾也搞自己的编码, ...
- 关于正则表达式RegExp
常用元字符串 元字符 说明 \d 匹配 数字 \D 匹配 非数字 \w 匹配 数字,字母,下划线 \W 匹配 任意不是字母,数字,下划线 \s 匹配 空白符 \S 匹配 任意不 ...