Re: [閒聊] 每日leetcode

看板Marginalman作者 (是oin的說)時間1年前 (2024/09/10 21:39), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串839/1548 (看更多)
題目 在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
文章代碼(AID): #1cu4ltgz (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cu4ltgz (Marginalman)