算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers

来源:互联网 发布:java中观察者模式 编辑:程序博客网 时间:2024/05/29 16:51

题目描述:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

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

使用C++解题给出的ListNode结构体定义:

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

这道题本身并不难,将数字反序存放到链表里也减弱了本题的难度。最一般的情况就是两个链表等长,每一位对应相加直到链表为空,注意考虑进位。此外,还要考虑两个链表不等长(一个链表为空可合并考虑)的情况,这里需要特殊考虑的是连续进位的情况:如[5,9,9,9]和[8,3]应得到[3,3,0,0,1]。在两个链表不等长时,必有一个链表先为空,这时可以直接将不为空的链表对应的后半部分接到结果上,然后再考虑进位,如若进位则一直进行计算,直到进位为0。


解法一(Time Limit Exceeded):

这里卡了很久,算法应该是没有问题的,代码应该也没有问题,但就是超时。最后改了很久,超时的原因应该是在第一个循环里对result是否为空的判断引起的计算缓慢(虽然还是想不明白为什么),最后将这部分去掉并做一些修改就通过了。

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode* result;        ListNode* result_head;        int carry = 0;        int temp;        // digits that l1 and l2 both contain (low digits)        while(l1 && l2){            // 这一部分就是超时的“罪魁祸首”            if(result == NULL){                result = new ListNode(0);                result_head = result;            }            else{                result->next = new ListNode(0);                result = result->next;            }            // 这一部分就是超时的“罪魁祸首”            temp = l1->val + l2->val + carry;            result->val = temp % 10;            carry = temp / 10;            l1 = l1->next;            l2 = l2->next;        }        // give the longer number remains to result        result->next = l1 ? l1 : l2;        // deal with carry        while(carry){            if(result->next == NULL){                result->next = new ListNode(1);                carry = 0;            }else{                result = result->next;                temp = result->val + carry;                carry = temp / 10;                result->val = temp % 10;            }        }        return result_head;    }};

解法二:

为了改正上面的错误,将标注部分去掉后需要对程序进行微调。采取的方法是在result前加一个节点,最后返回result_head->next,这样就可以避免在循环的开头不断对result是否为空进行判断了。最后用时52ms。

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode* result = new ListNode(0);  // 修改处        ListNode* result_head = result;  // 修改处        int carry = 0;        int temp;        // digits that l1 and l2 both contain (low digits)        while(l1 && l2){            result->next = new ListNode(0);            result = result->next;            temp = l1->val + l2->val + carry;            result->val = temp % 10;            carry = temp / 10;            l1 = l1->next;            l2 = l2->next;        }        // give the longer number remains to result        result->next = l1 ? l1 : l2;        // deal with carry        while(carry){            if(result->next == NULL){                result->next = new ListNode(1);                carry = 0;            }else{                result = result->next;                temp = result->val + carry;                carry = temp / 10;                result->val = temp % 10;            }        }        return result_head->next;    }};

解法三

一个十分简洁的版本,仅12行。为了使得代码更加简洁,将所有情况一起考虑,最后仅用了一个循环就解决了问题,虽然代码行数更少,但由于增加了计算时所需的比较次数,整体代码运行较慢,用时62ms。

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode* result = new ListNode(0);        ListNode* result_head = result;        int carry = 0; int sum = 0;        while(l1 || l2 || carry){            sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;            result->next = new ListNode(sum % 10);            carry = sum / 10;            result = result->next;            l1 = l1 ? l1->next : l1;            l2 = l2 ? l2->next : l2;        }        return result_head->next;    }};
0 0