Simple Python Solution add num1 to num2


  • 0
    class Solution(object):
        def addStrings(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            num1, num2 = list(map(int, num1[::-1])), list(map(int, num2[::-1]))
    
            if len(num1)<len(num2):
                num1, num2 = num2, num1
    
            carry = 0
            for i in range(len(num1)):
                n = num2[i] if i<len(num2) else 0
                tmp = n + carry + num1[i]
                num1[i] = tmp%10
                carry = tmp//10
    
            if carry:
                num1.append(1)
    
            return ''.join(map(str, num1))[::-1]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.