您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C++如何實(shí)現(xiàn)算法兩個(gè)數(shù)字相加”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C++如何實(shí)現(xiàn)算法兩個(gè)數(shù)字相加”這篇文章吧。
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.
LeetCode上的原題,請(qǐng)參考另一篇文檔Add Two Numbers 兩個(gè)數(shù)字相加。
跟那道LeetCode有所不同的是,這道題還有個(gè)Follow Up,把鏈表存的數(shù)字方向變了,原來是表頭存最低位,現(xiàn)在是表頭存最高位。既然是翻轉(zhuǎn)了鏈表,那么一種直接的解法是把兩個(gè)輸入鏈表都各自翻轉(zhuǎn)一下,然后用之前的方法相加完成,再把得到的結(jié)果翻轉(zhuǎn)一次,就是結(jié)果了,翻轉(zhuǎn)鏈表的方法可以參考另一篇文檔Reverse Linked List 倒置鏈表。代碼如下:
解法一:
// Follow up class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *dummy = new ListNode(-1); ListNode *cur = dummy; int carry = 0; l1 = reverseList(l1); l2 = reverseList(l2); while (l1 || l2) { int n1 = l1 ? l1->val : 0; int n2 = l2 ? l2->val : 0; int sum = n1 + n2 + carry; carry = sum / 10; cur->next = new ListNode(sum % 10); cur = cur->next; if (l1) l1 = l1->next; if (l2) l2 = l2->next; } if (carry) cur->next = new ListNode(1); return reverseList(dummy->next); } ListNode *reverseList(ListNode *head) { if (!head) return head; ListNode *dummy = new ListNode(-1); dummy->next = head; ListNode *cur = head; while (cur->next) { ListNode *tmp = cur->next; cur->next = tmp->next; tmp->next = dummy->next; dummy->next = tmp; } return dummy->next; } };
如果我們不采用翻轉(zhuǎn)鏈表的方法該怎么做呢,這就比較復(fù)雜了。首先我們要縣分別計(jì)算出兩個(gè)鏈表的長(zhǎng)度,然后給稍短一點(diǎn)的鏈表前面補(bǔ)0,補(bǔ)到和另一個(gè)鏈表相同的長(zhǎng)度。由于要從低位開始相加,而低位是鏈表的末尾,所以我們采用遞歸來處理,先遍歷到鏈表的末尾,然后從后面相加,進(jìn)位標(biāo)示符carry用的是引用,這樣保證了再遞歸回溯時(shí)值可以正確傳遞,每次計(jì)算的節(jié)點(diǎn)后面接上上一次回溯的節(jié)點(diǎn),直到回到首節(jié)點(diǎn)完成遞歸。最后還是處理最高位的進(jìn)位問題。代碼如下:
解法二:
// Follow up class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { int n1 = 0, n2 = 0, carry = 0;; n1 = getLength(l1); n2 = getLength(l2); if (n1 > n2) l2 = padList(l2, n1 - n2); if (n2 > n1) l1 = padList(l1, n2 - n1); ListNode *res = addTwoNumbersDFS(l1, l2, carry); if (carry == 1) { ListNode *tmp = new ListNode(1); tmp->next = res; res = tmp; } return res; } ListNode *addTwoNumbersDFS(ListNode *l1, ListNode *l2, int &carry) { if (!l1 && !l2) return NULL; ListNode *list = addTwoNumbersDFS(l1->next, l2->next, carry); int sum = l1->val + l2->val + carry; ListNode *res = new ListNode(sum % 10); res->next = list; carry = sum / 10; return res; } ListNode *padList(ListNode *list, int len) { ListNode *dummy = new ListNode(-1); ListNode *cur = dummy; for (int i = 0; i < len; ++i) { cur->next = new ListNode(0); cur = cur->next; } cur->next = list; return dummy->next; } int getLength(ListNode *list) { ListNode *cur = list; int res = 0; while (cur) { ++res; cur = cur->next; } return res; } };
以上是“C++如何實(shí)現(xiàn)算法兩個(gè)數(shù)字相加”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。