题目如下:

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. OC端代码

    ViewController.m #import "ViewController.h"#import <Flutter/Flutter.h>#include " ...

  2. 提取的js,要先部署在远程,再引入

    var meet = { _w: document.documentElement.clientWidth, _h: document.documentElement.clientWidth, ini ...

  3. vue里面的this指向

    this.$http.jsonp(api).then(function(response){ console.log(response); console.log(this); this.list=r ...

  4. day15—jQuery UI之widgets插件

    转行学开发,代码100天——2018-03-31 今天学习了jQuery UI的widgets插件,主要包括accordion插件 accordion插件 该插件表示折叠面板效果,点击头部展开/折叠被 ...

  5. 第 13 章 python并发编程之io模型

    一.IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...

  6. Java thread(1)

    这一部分主要讨论 java多线程的基本相关概念以及两种java线程的实现方式: 线程与进程: 这个操作系统书上介绍得很详细,这里就列出一些比较主要的: 线程: 线程本身有很少的资源,因为所拥有的资源较 ...

  7. deepin修改默认Python2到Python3

    第一步 打开终端 第二步 输入 sudo vi ~/.bashrc 然后你会看到如下界面: 切大写,输入E,进入如下界面,并在最后输入我已经输入的 alias python='python3' ,记住 ...

  8. document.domain vs location.hostname vs location.host

    限制是同源政策的相同规则 document.domain 获取域名 location.hostname    获取域名 location.host   获取域名+端口 document.domain ...

  9. [暑假集训Day4T1]羊圈

    ZYC同志开农场了????? 二分答案. 对于每一个二分出来的答案对其进行检查(check),检查是否有一个长度大于m的字段和的平均值大于mid.方法如下:先把原数组的每一个元素减去mid,储存进一个 ...

  10. 1.openshift搭建

    第1章 主机规划和所需文件 1.1 主机规划 IP地址 域名 用途 11.11.233.125 master01.song.test.cnpc 容器编排.etcd 11.11.233.126 mast ...