-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.add-two-numbers.py
152 lines (124 loc) · 3.96 KB
/
02.add-two-numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# https://leetcode-cn.com/problems/add-two-numbers
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution1:
'''
Date: 2022.03.30
Pass/Error/Bug: 1/3/0
执行用时:72 ms, 在所有 Python3 提交中击败了 15.90% 的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了 35.62% 的用户
'''
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
# 使用函数递归,增加进位功能
def addWithCarry(
l1: Optional[ListNode] = None,
l2: Optional[ListNode] = None,
carry: Optional[int] = 0
) -> ListNode:
val1, val2 = 0, 0
next1, next2 = None, None
flag1, flag2 = 0, 0
if l1 is not None:
val1 = l1.val
next1 = l1.next
flag1 = 1
if l2 is not None:
val2 = l2.val
next2 = l2.next
flag2 = 1
sum_val = val1 + val2 + carry
add_val = sum_val // 10
new_val = sum_val % 10
if (next1 is None) and (next2 is None):
if add_val != 0:
return ListNode(val=new_val, next=ListNode(val=add_val))
else:
return ListNode(val=new_val)
elif flag1 + flag2 > 0:
return ListNode(val=new_val, next=addWithCarry(next1, next2, add_val))
else:
return ListNode(val=new_val)
return addWithCarry(l1, l2)
class Solution2:
'''
Date: 2022.03.31
Pass/Error/Bug: 1/0/0
执行用时:68 ms, 在所有 Python3 提交中击败了 25.46% 的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了 12.26% 的用户
'''
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
n1s = []
n2s = []
while l1:
n1s.append(str(l1.val))
l1 = l1.next
while l2:
n2s.append(str(l2.val))
l2 = l2.next
n1s = int(''.join(n1s[::-1]))
n2s = int(''.join(n2s[::-1]))
s = n1s + n2s
prev = None
for i in str(s):
out = ListNode(int(i), prev)
prev = out
return out
class Solution3:
'''
Date: 2022.04.30
Pass/Error/Bug: 3/3/8
执行用时:60.0 ms, 在所有 Python3 提交中击败了 72.57% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了 82.98% 的用户
'''
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
idx = 0
carry = 0
head = ListNode(0)
cache1 = None
cache2 = None
while True:
if l1:
v1 = l1.val
if l1.next:
l1 = l1.next
else:
l1 = None
else:
v1 = 0
if l2:
v2 = l2.val
if l2.next:
l2 = l2.next
else:
l2 = None
else:
v2 = 0
sum_val = v1 + v2 + carry
add_val = sum_val // 10
new_val = sum_val % 10
if sum_val != 0:
if idx != 0:
cache2 = ListNode(new_val)
cache1.next = cache2
cache1 = cache2
else:
head = ListNode(new_val)
cache1 = head
carry = add_val
idx += 1
elif (l1 or l2):
if idx != 0:
cache2 = ListNode(0)
cache1.next = cache2
cache1 = cache2
else:
head = ListNode(0)
cache1 = head
idx += 1
else:
break
return head