题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
class Solution: def solve(self, s: str, t: str) -> str: # 反转字符串,使得可以从最低位开始相加 s, t = s[::-1], t[::-1] result = [] carry = 0 # 逐位相加,并处理进位 i, j = 0, 0 while i < len(s) or j < len(t) or carry: digit_s = int(s[i]) if i < len(s) else 0 digit_t = int(t[j]) if j < len(t) else 0 total = digit_s + digit_t + carry carry = total // 10 digit = total % 10 result.append(str(digit)) i += 1 j += 1 # 反转结果字符串并返回 return "".join(result[::-1])