My python solution


  • 0
    F
    class Solution(object):
        def addStrings(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            i, j = len(num1)-1, len(num2)-1
            res = []
            carry = 0
            while i>=0 and j>=0:
                carry, digit = divmod(int(num1[i])+int(num2[j])+carry, 10)
                res.append(digit)
                i -= 1
                j -= 1
            while i>=0:
                carry, digit = divmod(int(num1[i]) + carry, 10)
                res.append(digit)
                i -= 1
            while j>=0:
                carry, digit = divmod(int(num2[j])+carry, 10)
                res.append(digit)
                j -= 1
            if carry:
                res.append(carry)
            return ''.join(map(str, res[::-1]))
                
    
    

Log in to reply
 

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