【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 ...
随机推荐
- C++ 运算符优先级顺序表 (最新/完整)
优先级 运算符 结合律 助记 1 :: 从左至右 作用域 2 a++.a--.type().type{}.a().a[]...-> 从左至右 后缀自增减.函数风格转型.函数调用.下标.成员访问 ...
- [CSP-S模拟测试]:Market(背包DP)
题目描述 在比特镇一共有$n$家商店,编号依次为$1$到$n$.每家商店只会卖一种物品,其中第$i$家商店的物品单价为$c_i$,价值为$v_i$,且该商店开张的时间为$t_i$. $Byteasar ...
- jinjia2 模板学习
参考链接https://blog.csdn.net/langkew/article/details/51734423
- mglearn初探
这个是取自于<python机器学习基础教程>16页 代码: # import numpy as np # import matplotlib.pyplot as plt # import ...
- 基于vux的Vue路由切换动画
const history = window.sessionStorage history.clear() let historyCount = history.getItem('count') * ...
- Java_1.Java符号体系
Java符号包含五类:标识符.关键字.常量及字面量.运算符.分隔符 1.标识符 定义:用于标明程序中元素的名字,如类.方法和变量 命名规则: ·由字母.数字.下划线(_)和美元符号($)构成的字母序列 ...
- Vue对象的生命周期
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ioc和DI之间的关系(依赖注入的核心概念)
1.开篇闲话 由于之前做的很多项目都没接触到这个,后来到了另一个公司,他们的代码结构是基于领域驱动设计的,其中里面的对象都是通过依赖注入方式(Sprint.NET)实现的,也大致了解了哈,在网上搜了些 ...
- 【转】 关于form与表单提交操作的一切
参考一:http://caibaojian.com/form.html 参考二:https://blog.csdn.net/weixin_42301628/article/details/867156 ...
- elasticsearch 基础 —— 索引、更新文档
索引文档 通过使用 index API ,文档可以被 索引 -- 存储和使文档可被搜索 . 但是首先,我们要确定文档的位置.正如我们刚刚讨论的,一个文档的 _index . _type 和 _id 唯 ...