Skip to main content

Add Strings

描述​

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.

分析​

这题与 Add Binary 一模一样,将它的代码中的 2 改为 10 即可。

代码​

# Add Strings
# Time Complexity: O(max(m,n)), Space Complexity: O(max(m,n))
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
result = []
i, j = len(num1) - 1, len(num2) - 1
carry = 0
while i >= 0 or j >= 0 or carry > 0:
x = 0 if i < 0 else ord(num1[i]) - ord('0')
y = 0 if j < 0 else ord(num2[j]) - ord('0')
sum = x + y + carry
result.append(str(sum % 10))
carry = sum // 10
i, j = i - 1, j - 1
return ''.join(result[::-1])

相关题目​