閱讀189 返回首頁    go 京東網上商城


[LeetCode]2.Add Two Numbers

【題目】

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

【題意】

給你兩個鏈表,表示兩個非負整數。數字在鏈表中按反序存儲,例如342在鏈表中為2->4->3。鏈表每一個節點包含一個數字(0-9)。

計算這兩個數字和並以鏈表形式返回。

【分析】

【代碼1】

/*********************************
*   日期:2014-01-27
*   作者:SJF0115
*   題目: 2.Add Two Numbers
*   網址:https://oj.leetcode.com/problems/add-two-numbers/
*   結果:AC
*   來源:LeetCode
*   總結:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode *head = (ListNode *)malloc(sizeof(ListNode));
        ListNode *pre = head;
        ListNode *node = NULL;
        //進位
        int c = 0,sum;
        //加法
        while(l1 != NULL && l2 != NULL){
            sum = l1->val + l2->val + c;
            c = sum / 10;
            node = (ListNode *)malloc(sizeof(ListNode));
            node->val = sum % 10;
            node->next = NULL;
            //尾插法
            pre->next = node;
            pre = node;
            l1 = l1->next;
            l2 = l2->next;
        }
        //例如:2->4->3->1   5->6->4
        while(l1 != NULL){
            sum = l1->val + c;
            c = sum / 10;
            node = (ListNode *)malloc(sizeof(ListNode));
            node->val = sum % 10;
            node->next = NULL;
            //尾插法
            pre->next = node;
            pre = node;
            l1 = l1->next;
        }
        //例如:2->4->3   5->6->4->1
        while(l2 != NULL){
            sum = l2->val + c;
            c = sum / 10;
            node = (ListNode *)malloc(sizeof(ListNode));
            node->val = sum % 10;
            node->next = NULL;
            //尾插法
            pre->next = node;
            pre = node;
            l2 = l2->next;
        }
        //最後一位還有進位
        if(c > 0){
            node = (ListNode *)malloc(sizeof(ListNode));
            node->val = c;
            node->next = NULL;
            //尾插法
            pre->next = node;
            pre = node;
        }
        return head->next;
    }
};
int main() {
    Solution solution;
    int A[] = {2,4,7,9};
    int B[] = {5,6,4};
    ListNode *head = NULL;
    ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
    ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
    head1->next = NULL;
    head2->next = NULL;
    ListNode *node;
    ListNode *pre = head1;
    for(int i = 0;i < 4;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    pre = head2;
    for(int i = 0;i < 3;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = B[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.addTwoNumbers(head1->next,head2->next);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}


【代碼2】

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode *head = (ListNode *)malloc(sizeof(ListNode));
        ListNode *pre = head;
        ListNode *node = NULL;
        //進位
        int c = 0,sum,val1,val2;
        //加法
        while(l1 != NULL || l2 != NULL || c != 0){
            val1 = (l1 == NULL ? 0 : l1->val);
            val2 = (l2 == NULL ? 0 : l2->val);
            sum = val1 + val2 + c;
            c = sum / 10;
            node = (ListNode *)malloc(sizeof(ListNode));
            node->val = sum % 10;
            node->next = NULL;
            //尾插法
            pre->next = node;
            pre = node;
            l1 = (l1 == NULL ? NULL : l1->next);
            l2 = (l2 == NULL ? NULL : l2->next);
        }
        return head->next;
    }
};


最後更新:2017-04-03 12:54:49

  上一篇:go 端口檢測工具Fport的使用
  下一篇:go 客戶端的web技術