Re: [閒聊] 每日leetcode
題目
在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;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.22.67 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725975543.A.ABD.html
推
09/10 21:41,
1年前
, 1F
09/10 21:41, 1F
討論串 (同標題文章)
完整討論串 (本文為第 839 之 1548 篇):