-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path415.py
35 lines (29 loc) · 1.03 KB
/
415.py
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
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i = 0
length_num1 = len(num1)
length_num2 = len(num2)
add = 0
ans = ""
while (i < length_num1 and i < length_num2): # num1, num2 둘 다 더할 것이 남았을 때
x = ord(num1[length_num1 - 1 - i]) - ord("0")
y = ord(num2[length_num2 - 1 - i]) - ord("0")
t = x + y + add
add = t // 10
ans = str(t % 10) + ans
i += 1
while (i < length_num1): # num1만 더할 것이 남았을 때
x = ord(num1[length_num1 - 1 - i]) - ord("0")
t = x + add
add = t // 10
ans = str(t % 10) + ans
i += 1
while (i < length_num2): # num2만 더할 것이 남았을 때
x = ord(num2[length_num2 - 1 - i]) - ord("0")
t = x + add
add = t // 10
ans = str(t % 10) + ans
i += 1
if add:
ans = str(add) + ans
return ans