★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-pdhszdal-md.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Alice and Bob take turns playing a game, with Alice starting first.git
Initially, there is a number N
on the chalkboard. On each player's turn, that player makes a move consisting of:github
x
with 0 < x < N
and N % x == 0
.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.spa
Example 1:code
Input: 2
Output: true Explanation: Alice chooses 1, and Bob has no more moves.
Example 2:htm
Input: 3
Output: false Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
Note:blog
1 <= N <= 1000
爱丽丝和鲍勃一块儿玩游戏,他们轮流行动。爱丽丝先手开局。游戏
最初,黑板上有一个数字 N
。在每一个玩家的回合,玩家须要执行如下操做:ci
x
,知足 0 < x < N
且 N % x == 0
。N - x
替换黑板上的数字 N
。若是玩家没法执行这些操做,就会输掉游戏。
只有在爱丽丝在游戏中取得胜利时才返回 True
,不然返回 false
。假设两个玩家都以最佳状态参与游戏。
示例 1:
输入:2 输出:true 解释:爱丽丝选择 1,鲍勃没法进行操做。
示例 2:
输入:3 输出:false 解释:爱丽丝选择 1,鲍勃也选择 1,而后爱丽丝没法进行操做。
提示:
1 <= N <= 1000
1 class Solution { 2 func divisorGame(_ N: Int) -> Bool { 3 return N % 2 == 0 4 } 5 }
Runtime: 4 ms
1 class Solution { 2 func divisorGame(_ N: Int) -> Bool { 3 var N = N 4 var alice:Bool = false 5 for x in 1..<N 6 { 7 alice.toggle() 8 if N % x == 0 9 { 10 N -= x 11 } 12 } 13 return alice 14 } 15 }
1 class Solution { 2 func divisorGame(_ N: Int) -> Bool { 3 var dp = [Int:Bool]() 4 dp[1] = false 5 dp[2] = true 6 7 var i = 3 8 while i <= N{ 9 var x = i 10 var lose = false 11 dp[i] = false 12 repeat{ 13 x -= 1 14 if (i%x == 0 && dp[i-x]! == false){ 15 dp[i] = true 16 } 17 }while x > 1 18 19 i += 1 20 } 21 22 return dp[N]! 23 } 24 }