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

思路:用一个指针,循环遍历列表,模拟每个在场孩子报数,用cnt计数,当报到m-1后,将这个人出列,cnt置为0,剩下的人继续报数

# -*- coding:utf-8 -*-
class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        if n == 0 and m == 0:
            return -1
        child = [i for i in range(n)]
        p = 0
        cnt = 0
        while len(child) > 1:
            if cnt == m-1:
                child.pop(p)
                cnt = 0
            p = (p+1) % len(child)
            cnt += 1
        return child.pop()
原文地址:https://www.cnblogs.com/dolisun/p/11334348.html