forked from someshgkale123/LeetCode-Easy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Add Strings
27 lines (23 loc) · 786 Bytes
/
Add Strings
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
Problem Statement:
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly
Solution:
class Solution(object):
def addStrings(self, num1, num2):
c=0
stri=""
i=len(num1)-1
j=len(num2)-1
while (i>=0 or j>=0 or c):
val1=int(num1[i]) if i>=0 else 0
val2=int(num2[j]) if j>=0 else 0
summ=val1+val2+c
c,r=divmod(summ,10)
stri=str(r)+stri
i-=1
j-=1
return stri