forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-strings.py
More file actions
executable file
·34 lines (29 loc) · 858 Bytes
/
add-strings.py
File metadata and controls
executable file
·34 lines (29 loc) · 858 Bytes
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
class Solution(object):
def addStrings(self, nums1, nums2):
ans = ''
i = len(nums1)-1
j = len(nums2)-1
carry = 0
while 0<=i and 0<=j:
n1 = int(nums1[i])
n2 = int(nums2[j])
total = n1+n2+carry
n = total%10
carry = 1 if total>=10 else 0
ans = str(n)+ans
i -= 1
j -= 1
while 0<=i:
total = int(nums1[i])+carry
n = total%10
carry = 1 if total>=10 else 0
ans = str(n)+ans
i -= 1
while 0<=j:
total = int(nums2[j])+carry
n = total%10
carry = 1 if total>=10 else 0
ans = str(n)+ans
j -= 1
if carry: ans = str(carry)+ans
return ans