题解 | 孩子们的游戏(圆圈中最后剩下的数)

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

https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param n int整型 
# @param m int整型 
# @return int整型
#
class StuLinkedList:
    def __init__(self,student_num) -> None:
        self.stu_num = student_num
        self.next = None
class Solution:
    def LastRemaining_Solution(self , n: int, m: int) -> int:
        # write code here
        head = StuLinkedList(0)
        tmp_head= head
        i = 1
        while i < n :
            tmp = StuLinkedList(i)
            tmp_head.next = tmp
            tmp_head = tmp
            i += 1
            print(i,n)
            if i == n:
                tmp.next = head
        del_cnt = 0
        while del_cnt != n-1 :
            if del_cnt == n-1:
                break
            if m <2:
                tmp = head
            else:
                for i in range(m-2):
                    head = head.next
            tmp = head.next
            head.next = head.next.next
            head = head.next
            tmp.next = None
            del_cnt +=1
        return head.stu_num


        
            


全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务