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