发布于2019-08-18 11:40 阅读(1248) 评论(0) 点赞(13) 收藏(1)
链式A+B 牛客网 程序员面试金典 C++ Python
C++
/*
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };*/
class Plus {
public:
// run: 3ms memory:456k
ListNode* plusAB(ListNode* a, ListNode* b) {
ListNode* ret = new ListNode(0);
ListNode* p = ret;
int j=0;
while(NULL!=a || NULL!=b || j!=0){
ListNode* s = new ListNode(0);
s->val += j;
if (NULL!=a) s->val += a->val;
if (NULL!=a) a = a->next;
if (NULL!=b) s->val += b->val;
if (NULL!=b) b = b->next;
if (s->val >= 10) j=1; else j = 0;
if (s->val >=10) s->val -= 10;
p = p->next = s;
}
ListNode* r = ret->next;
free(ret);
return r;
}
// run:2ms memory:476k
ListNode* plusAB2(ListNode* a, ListNode* b) {
ListNode* ret = new ListNode(0);
ListNode* p = ret;
int j=0;
while(NULL!=a || NULL!=b || j!=0){
ListNode* s = new ListNode(0);
s->val += j;
if (NULL!=a) {
s->val += a->val;
a = a->next;
}
if (NULL!=b) {
s->val += b->val;
b = b->next;
}
if (s->val >= 10) {
j=1;
s->val -= 10;
}else{
j=0;
}
p->next = s;
p = p->next;
}
ListNode* r = ret->next;
free(ret);
return r;
}
// run:4 memory:608k
ListNode* plusAB3(ListNode* a, ListNode* b) {
ListNode* ret = new ListNode(0);
ListNode* p = ret;
int j=0;
while(NULL!=a && NULL!=b){
ListNode* s = new ListNode(0);
s->val = a->val + b->val + j;
if (s->val >= 10){
j = 1;
s->val -= 10;
}else
j = 0;
p->next = s;
p = p->next;
a = a->next;
b = b->next;
}
while(NULL!=a){
ListNode* s = new ListNode(0);
s->val = a->val + j;
if (s->val >= 10){
j = 1;
s->val -= 10;
}else
j = 0;
p->next = s;
p = p->next;
a = a->next;
}
while(NULL!=b){
ListNode* s = new ListNode(0);
s->val = b->val + j;
if (s->val >= 10){
j = 1;
s->val -= 10;
}else
j = 0;
p->next = s;
p = p->next;
b = b->next;
}
if (1 == j){
ListNode* s = new ListNode(1);
p->next = s;
}
ListNode* r = ret->next;
free(ret);
return r;
}
};
Python
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Plus:
#run: 27ms memory: 5728k
def plusAB(slef,a,b):
ret = ListNode(0)
p = ret
j = 0
while(a or b or j!=0):
s = ListNode(0)
s.val += j
if(a): s.val += a.val
if(a): a = a.next
if(b): s.val += b.val
if(b): b = b.next
if s.val >= 10: j = 1
else: j = 0
if s.val >= 10: s.val -= 10
p.next = s
p = p.next
return ret.next
#run: 37ms memory:5628k
def plusAB2(slef,a,b):
ret = ListNode(0)
p = ret
j = 0
while(a or b or j!=0):
s = ListNode(0)
s.val += j
if(a):
s.val += a.val
a = a.next
if(b):
s.val += b.val
b = b.next
if s.val >= 10:
j = 1
s.val -= 10
else:
j = 0
p.next = s
p = p.next
return ret.next
#run: 22ms memory:5856k
def plusAB3(self, a, b):
if None == a: return b
if None == b: return a
r = ListNode(0)
p = r
j = 0
while(a and b):
s = ListNode(0)
s.val = a.val + b.val + j
if s.val >= 10:
j = 1
s.val -= 10
else:
j = 0
p.next = s
p = p.next
a = a.next
b = b.next
while(a):
s = ListNode(0)
s.val = a.val + j
if s.val >= 10:
j = 1
s.val -= 10
else:
j = 0
p.next = s
p = p.next
a = a.next
while(b):
s = ListNode(0)
s.val = b.val + j
if s.val >= 10:
j = 1
s.val -= 10
else:
j = 0
p.next = s
p = p.next
b = b.next
if j == 1:
s = ListNode(1)
p.next = s
return r.next
作者:胡龙茶
链接:https://www.pythonheidong.com/blog/article/48719/0f8da0a84bb3bdcc2037/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!