selected_coding_interview/docs/415. 字符串相加.md
算法流程: 设定 i,j 两指针分别指向 num1,num2 尾部,模拟人工加法;
carry = tmp // 10,代表当前位相加是否产生进位;tmp = n1 + n2 + carry,并将当前位 tmp % 10 添加至 res 头部;i或j 走过数字首部后,给 n1,n2 赋值为 $0$,相当于给 num1,num2 中长度较短的数字前面填 $0$,以便后续计算。num1,num2 后跳出循环,并根据 carry 值决定是否在头部添加进位 $1$,最终返回 res 即可。复杂度分析:
<,,,,,,,>
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
res = ""
i, j, carry = len(num1) - 1, len(num2) - 1, 0
while i >= 0 or j >= 0:
n1 = int(num1[i]) if i >= 0 else 0
n2 = int(num2[j]) if j >= 0 else 0
tmp = n1 + n2 + carry
carry = tmp // 10
res = str(tmp % 10) + res
i, j = i - 1, j - 1
return "1" + res if carry else res
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder res = new StringBuilder("");
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
while(i >= 0 || j >= 0){
int n1 = i >= 0 ? num1.charAt(i) - '0' : 0;
int n2 = j >= 0 ? num2.charAt(j) - '0' : 0;
int tmp = n1 + n2 + carry;
carry = tmp / 10;
res.append(tmp % 10);
i--; j--;
}
if(carry == 1) res.append(1);
return res.reverse().toString();
}
}