用数组存2个数组,对应位相加,存储进位
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/19 14:36
# @Author : wutiande
class Solution():
def maxNUmAdd(self,str1:str,str2:str)->list:
length = max(len(str1),len(str2))
str1,str2 = f"%0{length}d"%(int(str1)),f"%0{length}d"%(int(str2)) # 高位补齐0
# print(str1,str2)
sl1,sl2 = [],[] # 存放数字
for i in range(length-1,-1,-1):
sl1.append(int(str1[i]))
sl2.append(int(str2[i]))
print(sl1,sl2)
carry = 0 # 进位
res = []
for i in range(length):
tempc = carry # 保存之前的进位
carry = (sl1[i]+sl2[i]+tempc)//10 # 进位
curSum = (sl1[i]+sl2[i]+tempc)-carry*10
# print(carry,curSum)
if i !=length-1:
res.append(curSum)
elif carry>0:
res.append(curSum)
res.append(carry)
else:
res.append(curSum)
res.reverse()
print(res)
if __name__ == '__main__':
s = Solution()
s.maxNUmAdd("1305687164","899839")