Skip to main content

🟡 剑指 Offer 62. 圆圈中最后剩下的数字

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解 1_难在数学推理.py#

class Solution:    def lastRemaining(self, n: int, m: int) -> int:        def dp(x):            if x == 1:                return 0            return (dp(x-1)+m) % x        return dp(n)

题解 2_迭代.py#

class Solution:    def lastRemaining(self, n: int, m: int) -> int:        ans = 0        for n in range(2, n+1):            ans = (ans+m) % n        return ans