给定两个字符串形式的非负整数 num1num2 ,计算它们的和并同样以字符串形式返回。

你不能使用任何內建的用于处理大整数的库(比如 BigInteger), 也不能直接将输入的字符串转换为整数形式。

 

示例 1:

输入:num1 = "11", num2 = "123"
输出:"134"

示例 2:

输入:num1 = "456", num2 = "77"
输出:"533"

示例 3:

输入:num1 = "0", num2 = "0"
输出:"0"

 

 

提示:

  • 1 <= num1.length, num2.length <= 104
  • num1num2 都只包含数字 0-9
  • num1num2 都不包含任何前导零

思路分析

模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def addStrings(self, nums1: str, nums2: str) -> str:
n = max(len(nums1),len(nums2))
nums1 = '0'*(n - len(nums1)) + nums1
nums2 = '0'*(n - len(nums2)) + nums2
s = ''
f = 0 #表示是否存在进位
for i in range(n-1,-1,-1):
t = int(nums1[i]) + int(nums2[i]) + f
f = t//10
t = t%10
s += str(t)
if f:
s+='1'
return s[::-1]

官解

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
res = ''
i, j, carry = len(num1) - 1, len(num2) - 1, 0
while i >= 0 or j >= 0:
n1 = int(num1[i]) if i >= 0 else 0
n2 = int(num2[j]) if j >= 0 else 0
tmp = n1 + n2 + carry
carry = tmp // 10
res = str(tmp % 10) + res
i -= 1
j -= 1
return '1' + res if carry > 0 else res