剑指offer-孩子们的游戏(圆圈中最后剩下的数)(python)

思路:
第一pop第i个。i=(0+m-1)%len(res)
第二次res变了,i从i后面一个开始计数,i=(i+m-1)%len(res)
最后剩一个拿出来

# -*- coding:utf-8 -*-
class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        if n==1:
            return n
        if n==0:
            return -1
        res=range(n)
        i=0
        while len(res)>1:
            i=(i+m-1)%len(res)
            res.pop(i)
        return res[0]

猜你喜欢

转载自blog.csdn.net/qq_42738654/article/details/104449596