Add Strings
Created: October 16, 2019 by [lek-tin]
Last updated: October 16, 2019
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note
- The length of both
num1
andnum2
is< 5100
. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
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:
def addStrings(self, num1: str, num2: str) -> str:
if len(num1) < len(num2):
return self.addStrings(num2, num1)
n, m = len(num1), len(num2)
if not n:
return num2
if not m:
return num1
res, carry, zero = "", 0, ord("0")
for i in range(n):
a, b = ord(num1[-i-1])-zero, ord(num2[-i-1])-zero if i <m else 0
print(a, b)
sum = carry + a + b
carry = sum // 10
res = chr(sum % 10 + zero) + res
if carry > 0:
res = chr(carry + zero) + res
return res