题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
#倒叙补0,然后对应相加并且把进位也加起来,相加后如果进位了把进位变成1,最后如果进位依旧有1再补到结果里,最后倒叙输出
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 计算两个数之和
# @param s string字符串 表示第一个整数
# @param t string字符串 表示第二个整数
# @return string字符串
#
class Solution:
def solve(self , s: str, t: str) -> str:
# write code here
temp = 0
res = ''
if len(s)==0:
return t
exit()
elif len(t)==0:
return s
exit()
s = s[::-1]
t = t[::-1]
bu = '0'*(abs(len(s)-len(t)))
if len(s)<len(t):
s= s+bu
else:
t = t+bu
for i in range(len(s)):
if temp ==1:
he = int(s[i])+int(t[i])+temp
else:
he = int(s[i])+int(t[i])
if he>=10:
res = res+ str(he)[1]
temp = 1
else:
res = res+ str(he)[0]
temp = 0
if temp == 1:
res = res+str(temp)
res = res[::-1]
return res
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 计算两个数之和
# @param s string字符串 表示第一个整数
# @param t string字符串 表示第二个整数
# @return string字符串
#
class Solution:
def solve(self , s: str, t: str) -> str:
# write code here
temp = 0
res = ''
if len(s)==0:
return t
exit()
elif len(t)==0:
return s
exit()
s = s[::-1]
t = t[::-1]
bu = '0'*(abs(len(s)-len(t)))
if len(s)<len(t):
s= s+bu
else:
t = t+bu
for i in range(len(s)):
if temp ==1:
he = int(s[i])+int(t[i])+temp
else:
he = int(s[i])+int(t[i])
if he>=10:
res = res+ str(he)[1]
temp = 1
else:
res = res+ str(he)[0]
temp = 0
if temp == 1:
res = res+str(temp)
res = res[::-1]
return res