题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475?tpId=295&tqId=1061819&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 计算两个数之和 # @param s string字符串 表示第一个整数 # @param t string字符串 表示第二个整数 # @return string字符串 # class Solution: def solve(self , s: str, t: str) -> str: # write code here s1 = list(s)[::-1] s2 = list(t)[::-1] s1 = list( map(int,s1)) s2 = list( map(int,s2)) maxlength=max(len(s1),len(s2))-1 minlength=min(len(s1),len(s2))-1 s3 = [0 for i in range(maxlength+2)] i=0 Acc=0 while i <= minlength: if s1[i]+s2[i]+Acc >= 10: s3[i]= s1[i]+s2[i]+Acc-10 i+=1 Acc=1 else: s3[i]= s1[i]+s2[i]+Acc Acc=0 i+=1 if len(s1)==i: while i<=maxlength: if s2[i]+Acc >=10: s3[i]=s2[i]+Acc -10 Acc=1 i+=1 else: Acc=0 i+=1 else: while i<=maxlength: if s1[i]+Acc >10: s3[i]=s1[i]+Acc -10 Acc=1 i+=1 else: Acc=0 i+=1 if Acc==1: s3[maxlength+1]=1 else: s3.pop() s3 =s3[::-1] s3 =list( map(str,s3)) return (str("".join(s3)))