Re: [閒聊] 每日leetcode

作者: DJYOMIYAHINA (通通打死)   2024-09-10 21:25:31
我好像第一次用gcd這個function
不過要我寫我也不知道怎麼寫
輾轉相除法好難
def insertGreatestCommonDivisors(self, head: Optional[ListNode]) ->
Optional[ListNode]:
headdd = head
while head:
pre = head
head = head.next
if head:
pre.next = ListNode(math.gcd(pre.val, head.val))
pre.next.next = head
return headdd
作者: nh60211as   2024-09-10 21:26:00
std::gcd
作者: sustainer123 (caster)   2024-09-10 21:27:00
if y==0 return x else return gcd(y,x%y)遞迴長這樣 迴圈我忘了

Links booklink

Contact Us: admin [ a t ] ucptt.com