本篇內(nèi)容介紹了“如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)與算法之兩數(shù)相加”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
為迎江等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及迎江網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、迎江網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
給定兩個(gè) 非空 鏈表來(lái)表示兩個(gè)非負(fù)整數(shù)。位數(shù)按照 逆序 方式存儲(chǔ),它們的每個(gè)節(jié)點(diǎn)只存儲(chǔ)單個(gè)數(shù)字。將兩數(shù)相加返回一個(gè)新的鏈表。
你可以假設(shè)除了數(shù)字 0 之外,這兩個(gè)數(shù)字都不會(huì)以零開頭。
示例:
輸入: (2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出: 7 -> 0 -> 8
原因: 342 + 465 = 807
1. Swift 語(yǔ)言
class AddTwoNumbers { func addTwoNumbers(l1: ListNode?, _ l2: ListNode?) -> ListNode? { var carry = 0, l1 = l1, l2 = l2 let dummy = ListNode(0) var node = dummy while l1 != nil || l2 != nil || carry != 0 { if l1 != nil { carry += l1!.val l1 = l1!.next } if l2 != nil { carry += l2!.val l2 = l2!.next } node.next = ListNode(carry % 10) node = node.next! carry = carry / 10 } return dummy.next } }
2. JavaScript 語(yǔ)言
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { var add = 0 , ans , head; while(l1 || l2) { var a = l1 ? l1.val : 0 , b = l2 ? l2.val : 0; var sum = a + b + add; add = ~~(sum / 10); var node = new ListNode(sum % 10); if (!ans) ans = head = node; else { head.next = node; head = node; } if (l1) l1 = l1.next; if (l2) l2 = l2.next; } if (add) { var node = new ListNode(add); head.next = node; head = node; } return ans; };
3. Python 語(yǔ)言
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): # maybe standard version def _addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ p = dummy = ListNode(-1) carry = 0 while l1 and l2: p.next = ListNode(l1.val + l2.val + carry) carry = p.next.val / 10 p.next.val %= 10 p = p.next l1 = l1.next l2 = l2.next res = l1 or l2 while res: p.next = ListNode(res.val + carry) carry = p.next.val / 10 p.next.val %= 10 p = p.next res = res.next if carry: p.next = ListNode(1) return dummy.next # shorter version def addTwoNumbers(self, l1, l2): p = dummy = ListNode(-1) carry = 0 while l1 or l2 or carry: val = (l1 and l1.val or 0) + (l2 and l2.val or 0) + carry carry = val / 10 p.next = ListNode(val % 10) l1 = l1 and l1.next l2 = l2 and l2.next p = p.next return dummy.next
4. Java 語(yǔ)言
public class ListNode { public int val; public ListNode next; public ListNode(int i) { this.val = i; } public int val() { return val; } } public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return dummyHead.next; }
5. C++ 語(yǔ)言
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { int x=0, y=0, carry=0, sum=0; ListNode *h=NULL, **t=&h; while (l1!=NULL || l2!=NULL){ x = getValueAndMoveNext(l1); y = getValueAndMoveNext(l2); sum = carry + x + y; ListNode *node = new ListNode(sum%10); *t = node; t = (&node->next); carry = sum/10; } if (carry > 0) { ListNode *node = new ListNode(carry%10); *t = node; } return h; } private: int getValueAndMoveNext(ListNode* &l){ int x = 0; if (l != NULL){ x = l->val; l = l->next; } return x; } };
6. C 語(yǔ)言
#include <stdio.h> #include <stdlib.h> #include <string.h> /* Definition for singly-linked list. */ struct ListNode { int val; struct ListNode *next; }; static struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { int carry_num = 0; int first = 1; struct ListNode *res = NULL; struct ListNode *p = NULL; struct ListNode *prev = p; while (l1 != NULL || l2 != NULL || carry_num) { int sum = 0; int last_carry = carry_num; if (l1 != NULL) { sum += l1->val; l1 = l1->next; } if (l2 != NULL) { sum += l2->val; l2 = l2->next; } if (sum >= 10) { sum -= 10; carry_num = 1; } else { carry_num = 0; } p = malloc(sizeof(*p)); if (first) { res = p; first = 0; } p->val = sum + last_carry; if (p->val >= 10) { p->val -= 10; carry_num = 1; } p->next = NULL; if (prev != NULL) { prev->next = p; } prev = p; } return res; } static struct ListNode *node_build(const char *digits) { struct ListNode *res, *p, *prev; int first = 1; int len = strlen(digits); const char *c = digits + len - 1; prev = NULL; while (len-- > 0) { p = malloc(sizeof(*p)); if (first) { first = 0; res = p; } p->val = *c-- - '0'; p->next = NULL; if (prev != NULL) { prev->next = p; } prev = p; } return res; } static void show(struct ListNode *ln) { int sum = 0, factor = 1; while (ln != NULL) { sum += ln->val * factor; factor *= 10; ln = ln->next; } printf("%d ", sum); } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Usage: ./test n1 n2 "); exit(-1); } struct ListNode *l1 = node_build(argv[1]); struct ListNode *l2 = node_build(argv[2]); struct ListNode *res = addTwoNumbers(l1, l2); show(l1); show(l2); show(res); return 0; }
“如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)與算法之兩數(shù)相加”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
分享名稱:如何實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)與算法之兩數(shù)相加
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article38/jjjepp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、外貿(mào)建站、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)