找出转圈游戏输家(数组计次)

力扣:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

示例代码:

class Solution:
    def circularGameLosers(self, n: int, k: int) -> List[int]:
        score = [False] * n  # 记录每个朋友的接球次数,初始为False
        i = 0  # 当前接球朋友编号,从0开始
        # 当前轮次,初始为r
        r = 1
        while not score[i]:
            score[i] = True
            i = (i + r * k) % n # 更新接球朋友编号
            r += 1  # 更新轮次
         # 从接球次数列表中筛选出未接球的朋友
        return [x+1 for x in range(n) if not score[x]]

猜你喜欢

转载自blog.csdn.net/weixin_44799217/article/details/132331427