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 提交记录