LeetCode 2. 两数相加

 

2. 两数相加

 

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807

您是否在真实的面试环节中遇到过这道题目呢? 

 

我的答案:

 

/**
 * 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) {
        unsigned long int res = 0;
        ListNode *tmplist = NULL;
        ListNode *reslist = NULL;
        int tmpten;
        
        tmpten = 1;
        tmplist = l1;
        res += tmplist->val;
        while(NULL != tmplist->next)
        {
            tmpten *= 10;
            tmplist = tmplist->next;
            res += tmplist->val * tmpten;
        }
        
        tmpten = 1;
        tmplist = l2;
        res += tmplist->val;
        while(NULL != tmplist->next)
        {
            tmpten *= 10;
            tmplist = tmplist->next;
            res += tmplist->val * tmpten;
        }
        
        tmplist = new ListNode(0);
        reslist = tmplist;
        tmplist -> val = res%10;
        while(res >= 10)
        {
            res /= 10;    
            tmplist->next = new ListNode(0);
            tmplist = tmplist->next;   
            tmplist->val = res%10;
        }
        
        return reslist;
    }
};

 

 

提交记录

1549 / 1562 个通过测试用例

状态:

解答错误

 

提交时间:0 分钟之前

输入: [9] [1,9,9,9,9,9,9,9,9,9]

输出: [8,0,4,5,6,0,0,1,4,1]

预期: [0,0,0,0,0,0,0,0,0,0,1]

 

我的答案2:

 

/**
 * 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) {
        unsigned long int res = 0;
        ListNode *tmplist = NULL;
        ListNode *reslist = NULL;
        unsigned long int tmpten;
        
        tmpten = 1;
        tmplist = l1;
        res += tmplist->val;
        while(NULL != tmplist->next)
        {
            tmpten *= 10;
            tmplist = tmplist->next;
            res += tmplist->val * tmpten;
        }
        
        tmpten = 1;
        tmplist = l2;
        res += tmplist->val;
        while(NULL != tmplist->next)
        {
            tmpten *= 10;
            tmplist = tmplist->next;
            res += tmplist->val * tmpten;
        }
        
        tmplist = new ListNode(0);
        reslist = tmplist;
        tmplist -> val = res%10;
        while(res >= 10)
        {
            res /= 10;    
            tmplist->next = new ListNode(0);
            tmplist = tmplist->next;   
            tmplist->val = res%10;
        }
        
        return reslist;
    }
};

 

 

提交记录

1560 / 1562 个通过测试用例

状态:

解答错误

 

提交时间:0 分钟之前

输入: [2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9] [5,6,4,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9,9,9,9]

输出: [7,0,2,7,8,5,1,6,7,8,4,3,5,5,0,4,8,4,0,1]

预期: [7,0,8,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,1,4,3,9,1]



 

我的答案3:

 

 

/**
 * 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) {
        int tmpres;
        int tmpten;
        ListNode *tmplist1 = l1;
        ListNode *tmplist2 = l2;
        ListNode *tmplistres = new ListNode(0);
        ListNode *reslist = tmplistres;
               
        tmpten = 0;
        while((NULL != tmplist1) || (NULL != tmplist2)  || 0!=tmpten)
        {
            if((NULL != tmplist1) && (NULL != tmplist2))
            {
                tmpres = tmplist1->val + tmplist2->val + tmpten;
                tmpten = tmpres/10;
                
                tmplistres->val = tmpres%10;         
                
                if((NULL !=tmplist1->next) || (NULL !=tmplist2->next) || 0!=tmpten)
                {
                    tmplistres->next = new ListNode(0);
                    tmplistres = tmplistres->next;
                }
                tmplist1 = tmplist1->next;
                tmplist2 = tmplist2->next;
            }
            else if(NULL != tmplist1)
            {
                tmpres = tmplist1->val + tmpten;
                tmpten = tmpres/10;
                
                tmplistres->val = tmpres%10;         
                
                if((NULL !=tmplist1->next) || 0!=tmpten)
                {
                    tmplistres->next = new ListNode(0);
                    tmplistres = tmplistres->next;
                }
                tmplist1 = tmplist1->next;
            }
            else if(NULL != tmplist2)
            {
                tmpres = tmplist2->val + tmpten;
                tmpten = tmpres/10;
                
                tmplistres->val = tmpres%10;         
                
                if((NULL !=tmplist2->next) || 0!=tmpten)
                {
                    tmplistres->next = new ListNode(0);
                    tmplistres = tmplistres->next;
                }
                tmplist2 = tmplist2->next;
            }
            else
            {
                tmpres = tmpten;
                tmpten = 0;                
                tmplistres->val = tmpres;   
            }
        }
        
        return reslist;
    }
};

 

 

提交记录

1562 / 1562 个通过测试用例

状态:

通过

执行用时:56 ms

提交时间:0 分钟之前


 

我的提交执行用时
已经战胜 31.59 % 的 cpp 提交记录

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值