码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Add Two Numbers

时间:2015-04-15 13:39:11      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   

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

解题思路:

这道题没有什么难点,注意最后一位进位的陷阱就成。我要做的就是如何更完美的表现出结果。主要是考虑首位相加和其他位相加情况不同。最开始,我用的是if语句。代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* p = l1, *q=l2;
        ListNode* result = new ListNode(0), *r=result;  //r记录当前的最高位
        int over = 0;   //进位
        bool first = true;  //第一位
        
        while(p!=NULL&&q!=NULL){
            int addNum = p->val + q->val + over;
            // addNum的范围在[0, 19]
            if(!first){
                r->next=new ListNode(0);
                r=r->next;
            }
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
            q=q->next;
            
            first = false;
        }
        
        if(q!=NULL){
            p=q;
        }
        
        while(p!=NULL){
            int addNum = p->val + over;
            if(!first){
                r->next=new ListNode(0);
                r=r->next;
            }
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
            
            first = false;
        }
        
        if(over!=0){
            r->next=new ListNode(0);
            r=r->next;
            r->val = over;
        }
        
        return result;
    }
};
这里想懒婆娘的裹脚布一样,不易明白。于是,第二版呼之欲出。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* result = new ListNode(0), *r=result;  //r记录当前的最高位
        
        result->val = (l1->val + l2->val)%10;
        
        int over = (l1->val + l2->val)/10;
        
        ListNode* p = l1->next, *q=l2->next;
        
        while(p!=NULL&&q!=NULL){
            int addNum = p->val + q->val + over;
            r->next=new ListNode(0);
            r=r->next;
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
            q=q->next;
        }
        
        if(q!=NULL){
            p=q;
        }
        
        while(p!=NULL){
            int addNum = p->val + over;
            r->next=new ListNode(0);
            r=r->next;
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
        }
        
        if(over!=0){
            r->next=new ListNode(0);
            r=r->next;
            r->val = over;
        }
        
        return result;
    }
};
比之前稍微好点,但是这里默认l1和l2至少有一个节点,仍然不美。第三版出来了。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* result = new ListNode(0), *r=result;  //r记录当前的最高位
        
        ListNode* p = l1, *q=l2;
        int over = 0;
        
        while(p!=NULL&&q!=NULL){
            int addNum = p->val + q->val + over;
            r->next=new ListNode(0);
            r=r->next;
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
            q=q->next;
        }
        
        if(q!=NULL){
            p=q;
        }
        
        while(p!=NULL){
            int addNum = p->val + over;
            r->next=new ListNode(0);
            r=r->next;
            r->val = addNum % 10;
            over = addNum / 10;
            
            p=p->next;
        }
        
        if(over!=0){
            r->next=new ListNode(0);
            r=r->next;
            r->val = over;
        }
        
        r = result;
        result = result->next;
        delete r;
        
        return result;
    }
};
个人比较喜欢这种处理方式,先假设有个头节点,之后的处理方式都是一样的,然后再删除头结点。


[LeetCode] Add Two Numbers

标签:c++   leetcode   

原文地址:http://blog.csdn.net/kangrydotnet/article/details/45056603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!