1.题目
2.题解
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
注意:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
双指针逆序模拟加法,注意进位。
public class Solution415 {
@Test
public void test415() {
String num1 = "10000";
String num2 = "9999";
System.out.println(addStrings(num1, num2));
}
public String addStrings(String num1, String num2) {
StringBuilder result = new StringBuilder();
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
while (i >= 0 || j >= 0) {
int t1 = i >= 0 ? num1.charAt(i) - '0' : 0;
int t2 = j >= 0 ? num2.charAt(j) - '0' : 0;
int tmp = t1 + t2 + carry;
carry = tmp / 10;
result.append(tmp % 10);
i--;
j--;
}
if (carry == 1) {
result.append(1);
}
return result.reverse().toString();
}
}