给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
不能直接将输入的字符串转换为整数形式。
思路:将字符串从最小位进行相加,最后形成的字符串倒置
class Solution {
public String addStrings(String num1, String num2) {
int i = () - 1, j = () - 1, add = 0;
int x,y,result;
StringBuffer ans = new StringBuffer();
while (i >= 0 || j >= 0 || add != 0) {
x = i >= 0 ? (i) - '0' : 0;
y = j >= 0 ? (j) - '0' : 0;
result = x + y + add;
(result % 10);
//add代表有无进位
add = result / 10;
i--;
j--;
}
//翻转
();
return ();
}
}