题解 | #大数加法#

大数加法

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

class Solution:
    def solve(self, s: str, t: str) -> str:
        # write code here
        max_length = max(len(s), len(t))
        result = ""
        flag = False
        gap = abs(len(s) - len(t))
        if len(s) > len(t):
            t = "0" * gap + t
        else:
            s = "0" * gap + s
        print("s:", s)
        print("t:", t)

        for i in range(max_length - 1, -1, -1):
            if flag == True:
                value = str(int(s[i]) + int(t[i]) + 1)
                result = value[-1] + result
                if int(s[i]) + int(t[i]) + 1 > 9:
                    flag = True
                else:
                    flag = False
            else:
                value = str(int(s[i]) + int(t[i]))[-1]
                result = value[-1] + result
                if int(s[i]) + int(t[i]) > 9:
                    flag = True
                else:
                    flag = False
        if flag == True:
            result = "1" + result

        return result

全部评论

相关推荐

10-17 16:07
门头沟学院 Java
牛牛大你18号:在汇报,突然弹出来,,领导以为我在准备跳槽,刚从领导办公室谈心出来
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务