Easy
按照我们数学上计算两个数相加的思路
10
要向前进一位以上,尝试一下代码,AC!
python3
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
if len(num1) < len(num2):
s,l = num1,num2
else:
s,l = num2,num1
car = 0
res = ''
rs = s[::-1]
rl = l[::-1]
for i in range(len(rl)):
t = int(rl[i]) + car
if i < len(rs):
t += int(rs[i])
res += str(t % 10)
car = 0
if t >= 10:
car += 1
return res[::-1] if car == 0 else '1' + res[::-1]