20200330-leetcode-面试题62. 圆圈中最后剩下的数字(约瑟夫环)

在这里插入图片描述
传送门
直接建立一个队列进行模拟。

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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章