- 字符串相加
模拟手算:
(1)注意对其最低项
(2)注意进位
public String addStrings(String num1, String num2) {
int i = num1.length() - 1;
int j = num2.length() - 1;
int add = 0;
StringBuilder sb = new StringBuilder();
while(i >= 0 || j >= 0 || flag) {
int x = i >= 0? num1.charAt(i) - '0' : 0;
int y = j >= 0? num2.charAt(j) - '0' : 0;
int sum = x + y + add;
i --;
j --;
sb.append(sum % 10);
add = sum / 10;
}
return sb.reverse().toString();
}
标签:num1,num2,int,相加,leetcode415,add,字符串,sum,String
From: https://www.cnblogs.com/k-young/p/16599812.html