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 in python:
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
def convert(s):
base = 1
result = 0
for i in range(len(s)-1, -1, -1):
result += base* int(s[i])
base *= 10
return result
def printout(n):
result = ""
if n == 0:
return "0"
while n > 0:
r = n % 10
n //= 10
result = str(r) + result
return result
return printout(convert(num1)+convert(num2))
留言