-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathadd-strings.py
More file actions
37 lines (36 loc) · 1.13 KB
/
add-strings.py
File metadata and controls
37 lines (36 loc) · 1.13 KB
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
class Solution:
"""
@param num1: a non-negative integers
@param num2: a non-negative integers
@return: return sum of num1 and num2
"""
def addStrings(self, num1, num2):
# write your code here
ret = []
l1, l2 = len(num1), len(num2)
num1 = num1[::-1]
num2 = num2[::-1]
step = 0
for i in range(min(l1, l2)):
v = (ord(num1[i]) - ord('0')) + (ord(num2[i]) - ord('0')) + step
if v >= 10:
ret.append(chr(v - 10 + ord('0')))
step = 1
else:
ret.append(chr(v + ord('0')))
step = 0
start, num = l1, num2
if l2 < l1:
start, num = l2, num1
for i in range(start, len(num)):
v = (ord(num[i]) - ord('0')) + step
if v >= 10:
ret.append(chr(v - 10 + ord('0')))
step = 1
else:
ret.append(chr(v + ord('0')))
step = 0
if step == 1:
ret.append('1')
return ''.join(ret[::-1])
# easy: https://www.lintcode.com/problem/add-strings/