【leetcode 简单】 第九十九题 字符串相加
发布时间:2020-12-14 03:48:03 所属栏目:大数据 来源:网络整理
导读:给定两个字符串形式的非负整数? num1 和 num2 ?,计算它们的和。 注意: num1 和 num2 ?的长度都小于 5100. num1 和 num2 都只包含数字? 0-9 . num1 和 num2 都不包含任何前导零。 你不能使用任何內建 BigInteger 库,?也不能直接将输入的字符串转换为整数形
给定两个字符串形式的非负整数? 注意:
class Solution: def addStrings(self,num1,num2): """ :type num1: str :type num2: str :rtype: str """ a,b=len(num1)-1,len(num2)-1 tmp = 0 result = ‘‘ while a>=0 or b>=0: if a>=0: tmp += ord(num1[a]) - ord(‘0‘) if b >=0: tmp += ord(num2[b]) - ord(‘0‘) result += chr(tmp%10 + ord(‘0‘)) tmp //= 10 a-=1 b-=1 if tmp == 1: result += ‘1‘ return result[::-1] 参考: ??????? https://www.polarxiong.com/archives/LeetCode-415-add-strings.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |