-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path415.py
More file actions
45 lines (40 loc) · 1.02 KB
/
415.py
File metadata and controls
45 lines (40 loc) · 1.02 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
38
39
40
41
42
43
44
45
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
len1 = len(num1)
len2 = len(num2)
if len1 is 0:
return num2
if len2 is 0:
return num1
ans = []
flag = 0
while len1 and len2:
count = int(num1[len1-1]) + int(num2[len2-1])+flag
flag = count/10
ans.append(str(count%10))
len1 -= 1
len2 -= 1
print len1, len2
if len1 > 0:
while len1:
count = int(num1[len1-1])+flag
flag = count/10
ans.append(str(count%10))
len1 -= 1
if len2 > 0:
while len2:
count = int(num2[len2-1])+flag
flag = count/10
ans.append(str(count%10))
len2 -= 1
if flag > 0:
ans.append(str(flag))
ans.reverse()
return "".join(ans)
test = Solution()
print test.addStrings('6','501')