傳送門
直接建立一個隊列進行模擬。
class Solution(object):
def lastRemaining(self, n, m):
"""
:type n: int
:type m: int
:rtype: int
"""
list = []
if n == 1:
return
for i in range(n):
list.append(i)
pos=0
for i in range(n - 1):
pos += (m-1)
pos = pos % (len(list))
# print(pos)
list.pop(pos)
return list[0]