【leetcode】1025. Divisor Game
题目如下:
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
with0 < x < N
andN % x == 0
.- Replacing the number
N
on the chalkboard withN - 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 <= 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 ...
随机推荐
- 元素隐藏visibility:hidden与元素消失display:none的区别
visibility属性用来确定元素是显示还是隐藏的,这用visibility="visible|hidden"来表示(visible表示显示,hidden表示隐藏). 当visi ...
- 洛谷P1242 新汉诺塔(dfs,模拟退火)
洛谷P1242 新汉诺塔 最开始的思路是贪心地将盘子从大到小依次从初始位置移动到目标位置. 方法和基本的汉诺塔问题的方法一样,对于盘子 \(i\) ,将盘子 \(1\to i-1\) 放置到中间柱子上 ...
- edusoho迁移
1.目录指向web,index文件设为app.php <VirtualHost *:8000> ServerAdmin abcd@mail.com DocumentRoot "E ...
- Oracle Data Guard Protection Modes
Maximum Availability This protection mode provides the highest level of data protection that is poss ...
- CentOS 7.0 开端口
>>> CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下1.直接关闭防火墙systemctl stop firewalld.serv ...
- instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口、抽象类、父类)的实例。
http://lavasoft.blog.51cto.com/62575/79864/ 深入Java关键字instanceof 2008-06-02 07:50:43 标签:Java 关键字 休 ...
- 16/7/11_PHP-PHP异常处理
抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出之后,后面的代码将不会再被执行. 既然抛出异常会中断程序执行,那么为什么 ...
- Leapin' Lizards [HDU - 2732]【网络流最大流】
题目链接 网络流直接最大流就是了,只是要拆点小心一个点的流超出了原本的正常范围才是. #include <iostream> #include <cstdio> #includ ...
- spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)
转载自 spring事务——try{...}catch{...}中事务不回滚的几种处理方式 当希望在某个方法中添加事务时,我们常常在方法头上添加@Transactional注解 @Respon ...
- Scala操作外部数据
Scala操作外部数据: 1.操作文件 2.操作XML 3.操作MySQL 读取文件: object FileApp { def main(args: Array[String]): Unit = { ...