提交 5a4a332f 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:字符串相加

上级 c27bd5d8
"""
字符串相加
"""
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
"""
双指针
:param num1:
:param num2:
:return:
"""
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
all = n1 + n2 + carry
carry = all // 10
res = str(all % 10) + res
i -= 1
j -= 1
return "1" + res if carry else res
if __name__ == '__main__':
result = Solution().addStrings("11", "123")
print(result)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册