-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path415.py
More file actions
29 lines (29 loc) · 878 Bytes
/
Copy path415.py
File metadata and controls
29 lines (29 loc) · 878 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
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
m, n = len(num1), len(num2)
longer, shorter = list(num1), list(num2)
if m < n:
longer, shorter = shorter, longer
m, n = n, m
i, carry = 1, 0
while n - i >= 0:
sum = ord(longer[m - i]) - ord('0') + ord(shorter[n - i]) - ord('0') + carry
carry = sum // 10
sum %= 10
longer[m - i] = str(sum)
i += 1
while carry:
if m - i < 0:
longer = ['1'] + longer
break
sum = ord(longer[m - i]) - ord('0') + carry
carry = sum // 10
sum %= 10
longer[m - i] = str(sum)
i += 1
return "".join(longer)