题解 | #大数加法#
大数加法
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