代码拉取完成,页面将自动刷新
# Time: O(n)
# Space: O(1)
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
result = []
i, j, carry = len(num1) - 1, len(num2) - 1, 0
while i >= 0 or j >= 0 or carry:
if i >= 0:
carry += ord(num1[i]) - ord('0')
i -= 1
if j >= 0:
carry += ord(num2[j]) - ord('0')
j -= 1
result.append(str(carry % 10))
carry /= 10
result.reverse()
return "".join(result)
def addStrings2(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
length = max(len(num1), len(num2))
num1 = num1.zfill(length)[::-1]
num2 = num2.zfill(length)[::-1]
res, plus = '', 0
for index, num in enumerate(num1):
tmp = str(int(num) + int(num2[index]) + plus)
res += tmp[-1]
if int(tmp) > 9:
plus = 1
else:
plus = 0
if plus:
res += '1'
return res[::-1]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。