-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd_Strings.py
More file actions
33 lines (27 loc) · 762 Bytes
/
Add_Strings.py
File metadata and controls
33 lines (27 loc) · 762 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
30
31
32
33
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i=len(num1)-1
j=len(num2)-1
result=""
carry=0
while i>=0 or j>=0 or carry!=0:
if i>=0:
n1=ord(num1[i]) - ord('0')
else:
n1=0
if j>=0:
n2= ord(num2[j]) - ord('0')
else:
n2=0
total=n1 + n2 + carry
digit= total % 10
carry= total // 10
result=chr(digit + ord('0'))+ result
i-=1
j-=1
return result
# Example usage:
sol = Solution()
print(sol.addStrings("123", "456"))
print(sol.addStrings("11", "123"))
print(sol.addStrings("0", "0"))