题解 | #牛牛的冒险旅程#

牛牛的冒险旅程

https://www.nowcoder.com/practice/79f7bf3d985c4b33af6d048ab53848d6

import numbers
import re
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return int整型
#
from functools import reduce
class Solution:
    def gcdInCycle(self , head: ListNode) -> int:
        # write code here
        nums=[]
        cur=head
        dic={}
        while cur:
            if not dic.get(cur.val):
                dic[cur.val]=1
            else:
                nums.append(cur.val)
                dic[cur.val]+=1
                if dic[cur.val]==3:
                    return reduce(self.gcd,nums)
            cur=cur.next
        return -1
    
    def gcd(self,x,y):
        while y:
            x,y=y,x%y
        return x

全部评论

相关推荐

qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
04-17 10:16
门头沟学院 Java
小浪_coder:24届很难找了,马上25的都毕业了还有很多没找到的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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