python--剑指offer--简单--62. 圆圈中最后剩下的数字
程序员文章站
2022-04-03 21:19:11
...
class Solution:
def lastRemaining(self, n: int, m: int) -> int:
pos = 0 # 最终活下来那个人的初始位置。最终留下的元素的位置为0
for i in range(2, n + 1):
pos = (pos + m) % i # 每次循环右移
return pos
if __name__ == '__main__':
solution = Solution()
n, m = 5, 3
n, m = 10, 17
res = solution.lastRemaining(n, m)
print(res)