Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-09-10 21:39:01
題目
在listnode中間插入他們的gcd值
思路
分成三個部分
gcd
插入
遍歷節點
然後寫進去就好了
class Solution {
public:
int gcd(int a , int b)
{
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void in(ListNode* a ,ListNode* b)
{
ListNode* c = new ListNode(gcd(a->val,b->val) , b);
a->next = c;
}
ListNode* insertGreatestCommonDivisors(ListNode* head)
{
ListNode* n = head;
ListNode* p ;
while(n!=NULL)
{
p = n;
n = n->next;
if(n == NULL)return head;
in(p,n);
}
return head;
}
};
```
作者: mrsonic (typeB)   2024-09-10 21:41:00
幾點了 你有甚麼用

Links booklink

Contact Us: admin [ a t ] ucptt.com