C++實(shí)現(xiàn)算法兩個(gè)數(shù)字相加詳解
Add Two Numbers 兩個(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ù)字方向變了,原來(lái)是表頭存最低位,現(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)度。由于要從低位開始相加,而低位是鏈表的末尾,所以我們采用遞歸來(lái)處理,先遍歷到鏈表的末尾,然后從后面相加,進(jìn)位標(biāo)示符carry用的是引用,這樣保證了再遞歸回溯時(shí)值可以正確傳遞,每次計(jì)算的節(jié)點(diǎn)后面接上上一次回溯的節(jié)點(diǎn),直到回到首節(jié)點(diǎn)完成遞歸。最后還是處理最高位的進(jìn)位問(wè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;
}
};
到此這篇關(guān)于C++實(shí)現(xiàn)算法兩個(gè)數(shù)字相加詳解的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)兩個(gè)數(shù)字相加內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)系列篇二叉樹的概念及滿二叉樹與完全二叉樹
在上一章中我們正式開啟了對(duì)數(shù)據(jù)結(jié)構(gòu)中樹的講解,介紹了樹的基礎(chǔ)。本章我們將學(xué)習(xí)二叉樹的概念,介紹滿二叉樹和完全二叉樹的定義,并對(duì)二叉樹的基本性質(zhì)進(jìn)行一個(gè)簡(jiǎn)單的介紹。本章附帶課后練習(xí)2022-02-02
C++執(zhí)行shell命令的多種實(shí)現(xiàn)方法
在linux系統(tǒng)下,用C++程序執(zhí)行shell命令有多種方式,主要介紹了3中方法,具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11
C語(yǔ)言實(shí)現(xiàn)教務(wù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)教務(wù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C++實(shí)現(xiàn)LeetCode(186.翻轉(zhuǎn)字符串中的單詞之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(186.翻轉(zhuǎn)字符串中的單詞之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
QT通過(guò)C++線程池運(yùn)行Lambda自定義函數(shù)流程詳解
最近在接觸公司的一個(gè)QT桌面項(xiàng)目,其中里面有一個(gè)模塊是使用線程池去運(yùn)行自定義函數(shù)的,自己潛心研究那個(gè)線程池代碼一天,發(fā)現(xiàn)研究不透,看不懂,里面幾乎都是使用C++11的新特性進(jìn)行編寫2022-10-10
C++?反匯編之關(guān)于Switch語(yǔ)句的優(yōu)化措施
這篇文章主要介紹了C++?反匯編之關(guān)于Switch語(yǔ)句的優(yōu)化措施,利用三種優(yōu)化來(lái)降低樹高度,誰(shuí)的效率高就優(yōu)先使用誰(shuí),三種優(yōu)化都無(wú)法匹配才會(huì)使用判定樹,具體內(nèi)容詳情跟隨小編一起看看吧2022-01-01
C++可變參數(shù)函數(shù)的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于C++可變參數(shù)函數(shù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

