AcWing 82. 圆圈中最后剩下的数字
原题链接
简单
作者:
zc2077
,
2020-07-13 10:46:30
,
所有人可见
,
阅读 654
- 模拟
class Solution(object):
def lastRemaining(self, n, m):
"""
:type n: int
:type m: int
:rtype: int
"""
nums = list(range(n))
while len(nums) != 1:
index = m % len(nums) - 1 if m % len(nums) != 0 else len(nums) - 1
nums = nums[index + 1:] + nums[:index]
return nums[0]
- 递归
# n>=1,最后一次为f(1, m) = 0,取余数从2开始;
class Solution(object):
def lastRemaining(self, n, m):
"""
:type n: int
:type m: int
:rtype: int
"""
start = 0
for i in range(2, n + 1):
start = (start + m) % i
return start