Skip to content

Instantly share code, notes, and snippets.

@humpydonkey
Created April 7, 2021 01:10
Show Gist options
  • Save humpydonkey/8bd270b40f940949e09b639af53553f6 to your computer and use it in GitHub Desktop.
Save humpydonkey/8bd270b40f940949e09b639af53553f6 to your computer and use it in GitHub Desktop.
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
res = []
len1, len2 = len(num1), len(num2)
carry = 0
i = 0
while i < len1 or i < len2:
sum = carry
if i < len1:
sum += int(num1[len1-1-i])
if i < len2:
sum += int(num2[len2-1-i])
if sum >= 10:
carry = 1
sum = sum % 10
else:
carry = 0
res.append(sum)
i += 1
if carry:
res.append(carry)
return ''.join(str(x) for x in res[::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment