题目如下:

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number N on the chalkboard.  On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < N and N % x == 0.
  • Replacing the number N on the chalkboard with N - x.

Also, if a player cannot make a move, they lose the game.

Return True if 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. 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的更多相关文章

  1. 【LeetCode】1025. Divisor Game 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 动态规划 日期 题目地址:https://l ...

  2. 【Leetcode_easy】1025. Divisor Game

    problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. [CSP-S模拟测试]:巨神兵(状压DP)

    题目描述 欧贝利斯克的巨神兵很喜欢有向图,有一天他找到了一张$n$个点$m$条边的有向图.欧贝利斯克认为一个没有环的有向图是优美的,请问这张图有多少个子图(即选定一个边集)是优美的?答案对$1,000 ...

  2. 【Java架构:进阶技术】——一篇文章搞掂:JVM调优

    Sun官方定义的Java技术体系: Java程序设计语言 各种硬件平台上的Java虚拟机 Class文件格式 Java API类库 来自商业机构和开源社区的第三方Java类库 JDK(Java Dev ...

  3. unity项目警告之 LF CRLF问题

    unity中创建的脚本,以LF结尾. Visual studio中创建的脚本,以 CRLF结尾. 当我们创建一个unity脚本后,再用VS打开编辑保存后,这个文件既有LF结尾符,也有CRLF结尾符. ...

  4. Java连接MySql数据库之JDBC

    1.首先创建一个java Project项目 2.起一个英文的项目名 3.此窗口点击NO 4.此时项目状态如下 5.创建一个文件夹,并将mysql-connector-java-5.1.8-bin.j ...

  5. day26—JavaScript对CSS样式的获取和修改实践

    转行学开发,代码100天——2018-04-11 通过JavaScript获取和修改HTML元素及CSS属性是其一个基本功能.对于CSS样式通常有行内样式,外部样式,内嵌样式之分. 如: 行内样式: ...

  6. TensorFlow学习笔记1-入门

    TensorFlow学习笔记1-入门 作者: YunYuan *** 写在前面 本笔记是我学习TensorFlow官方文档中文版的读书笔记,由于尚未搭建好Github的个人博客的评论功能,故尚不方便与 ...

  7. Win7 VSCode 离线安装Rust语言及环境配置

    前置依赖 装过Visual Studio或Visual Studio Build Tool 2015 下载Rust离线安装包 https://forge.rust-lang.org/other-ins ...

  8. Python入门习题10.河内塔(汉诺塔)问题

    例10 共n个圆盘,a,b,c三根柱子 #汉诺塔问题.py def Hanoi(n): #定义n阶汉诺塔问题移动次数函数 if n == 1: return 1 else: return 2*Hano ...

  9. python爬取企业登记业务

    import requests from lxml import etree import csv for i in range(10, 990, 10): url = "http://12 ...

  10. jenkin 构建失败 才发邮件通知

    使用场景:自动化测试,一般需要配置定时执行(每天执行一次,没周执行一次),如果有失败,则发邮件给相关人员关注.此时需要使用jenkins的邮件发送配置.修改job的configure配置步骤如下: 1 ...