题解 | #大数加法#

大数加法

https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475

import re
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 计算两个数之和
# @param s string字符串 表示第一个整数
# @param t string字符串 表示第二个整数
# @return string字符串
#
class Solution:
    def solve(self , s: str, t: str) -> str:
        # write code here
        if s=="":
            return t
        if t=="":
            return s
        tem=0
        res=[]
        ss=list(s)
        tt=list(t)
        while len(ss)<len(tt):
            ss.insert(0,'0')
        while len(ss)>len(tt):
            tt.insert(0,'0')
        lenlen=len(tt)
        for i in reversed(range(lenlen)):
            sum=int(ss[i])+int(tt[i])+tem
            if sum>=10:
                tem=1
            else:
                tem=0
            res.insert(0,str(sum%10))
        if tem==1:
            res.insert(0,"1")
        res="".join(res)
        return res

        

我不确定,很多解题中的转换成 int 做加法是不是面试官想看到的,我认为不是

全部评论

相关推荐

点赞 评论 收藏
分享
09-29 15:34
已编辑
北京航空航天大学 C++
做个有文化的流氓:结果是好的,过程不重要,而且你的offer太多了
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务