【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 ...
随机推荐
- Facebook发布Tweaks:让微调iOS应用变得更简单
假设,你正在开发一款iOS应用. 你的iOS应用有很多动画效果,而你(或你的设计师)希望让那些动画效果的持续时间恰到好处.那华丽的抽屉特效是应该耗时半秒钟,还是四分之三秒呢? 通常情况下,开发者会对合 ...
- vue定义自定义事件方法、事件传值及事件对象
1.自定义事件 例如v-on:click="run" 或者 @click="run" <template> <div id="app ...
- css3的各种属性的讲解
1.渐变(gradients) 水平渐变:linear gradient 语法:background:linear-gradient(direction,color1,color2); directi ...
- EZOJ #389点分治好题
分析 一层一层把叶子去掉 看最多能去掉多少层即可 代码 #include<bits/stdc++.h> using namespace std; ],du[],fa[],n,m,ans; ...
- 140、spring webflux 高并发的spring组件
最近公司可谓是风云变幻,年前说要拆开卖,后来说要整体卖,表示像我这种渣渣,始终逃脱不掉被卖的命运 下面进入正题 spring webflux 是spring 支持的高并发web框架,将每个http请求 ...
- 像计算机科学家一样思考python-第1章 程序之道
1.7调试 程序是很容易出错的.因为某种古怪的原因,程序错误被称为bug,而查捕bug的过程称为调试(debugging). 一个程序中可能出现3种类型的错误:语法错误.运行时错误和语义错误.对它们加 ...
- (appium+python)UI自动化_02_appium启动手机app
前提:需先安装配置好appium+python自动化环境,已配置好环境的小伙伴可以参考以下步骤启动Android app,具体步骤如下: 一.USB连接手机 (1)手机USB连接电脑 (2)手机打开开 ...
- java web项目启动加载顺序
转载:https://www.cnblogs.com/writeLessDoMore/p/6935524.html web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:T ...
- LeetCode 144. Binary Tree Preorder Traversal 动态演示
先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...
- Jmeter JAVA请求入门
一.Jmeter完成一个java请求实现方法 两种实现方式: 实现JavaSamplerClient接口 继承AbstractJavaSamplerClient抽象类 二.使用AbstractJava ...