leetcode-面试题62. 圆圈中最后剩下的数字

题目链接

https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/submissions/

leetcode-面试题62. 圆圈中最后剩下的数字_第1张图片

暴力法

构造列表l = list(range(n))
初始化索引index = 0
当l > 1时,index += m,index = (index-1+m)% len(l)

class Solution:
    def lastRemaining(self, n: int, m: int) -> int:
        index = 0
        l = list(range(n))

        while len(l) > 1:
            index = (index-1+m) % len(l)
            del l[index]

        return l[0]

你可能感兴趣的:(leetcode)