题解 | #大数加法#
大数加法
http://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
class Solution:
def solve(self , s , t ):
# write code here
# mimic the plus calculation. not allowed using inte converter
#add=0
#fill zero in shorter string to make same length and then we can
# perform add index by index
# time complexity O(n) n is the max length
max_len= len(s) if len(s)>len(t) else len(t)
s=s.zfill(max_len)
t=t.zfill(max_len)
add = 0
res=''
for i in range(max_len-1,-1,-1):
temp=int(s[i])+int(t[i])+add
res=res+str(temp%10) #0
add=temp//10
if add>0:
res=res+str(add)
new_res=''
for i in range(len(res)-1,-1,-1):
new_res+=res[i]
return new_res