Created
April 7, 2021 01:10
-
-
Save humpydonkey/8bd270b40f940949e09b639af53553f6 to your computer and use it in GitHub Desktop.
Saved from https://leetcode.com/problems/add-strings/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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