// 冲刺026
class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1, add = 0;
StringBuffer ans = new StringBuffer();
while (i >= 0 || j >= 0 || add != 0) {
int x = i >= 0 ? num1.charAt(i) - '0' : 0;
int y = j >= 0 ? num2.charAt(j) - '0' : 0;
int result = x + y + add;
ans.append(result % 10);
add = result / 10;
i--;
j--;
}
ans.reverse();
return ans.toString();
}
}
Leetcode_415_字符串相加_模拟
最新推荐文章于 2023-04-28 17:01:22 发布
该博客介绍了如何使用Java实现两个字符串数字相加的功能。通过从字符串尾部开始遍历,逐位相加并处理进位,最后得到结果。这种方法涉及字符串处理和基本算术运算。

789

被折叠的 条评论
为什么被折叠?



