Given two non-negative numbers 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 are must not with any built-in BigInteger library or convert the inputs to integer directly.
Subscribe to the which companies asked this question
This is a straightforward one, and the last line of comments is a handy python integer type.
The total feeling program where write a bit ugly ~ ~ ~ ~
Class Solution (object):
def addstrings (self, NUM1, num2):
l1 = Len (num1)
L2 = Len (num2)
lmin = min (l1,l2
Lmax = max (L1,L2)
res = [0] * (lmax+1)
#print res for
i in Xrange (1,lmin+1):
res[-i] = Int (num1[-i)) + int (num2[-i])
#print i,res
if L1 > L2: For
i in Xrange (lmin+1,lmax+1):
res[-i] = Int (num1[-i))
else: For
i-xrange (lmin+1,lmax+1):
res[-i] = Int (num2[-i)) for
I in Xrange (1,lmax+1):
If Res [I] > 9:
res[-i] =
Res[-i-1] + = 1
rres = ' for
i in res:
rres + = str (i)
if rres[0] = = ' 0 ':
rres = rres[1:] return
rres
#return str (int (NUM1) + int (num2))