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