给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
    提示:

    1. num1 和num2 的长度都小于 5100
    2. num1 和num2 都只包含数字 0-9
    3. num1 和num2 都不包含任何前导零
    4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式

    思路:

    主要注意点:char - ‘0’ 将字符转换为数字

    1. class Solution {
    2. public String addStrings(String num1, String num2) {
    3. int i = num1.length() - 1, j = num2.length() - 1, add = 0;
    4. StringBuffer ans = new StringBuffer();
    5. while (i >= 0 || j >= 0 || add != 0) {
    6. int x = i >= 0 ? num1.charAt(i) - '0' : 0;
    7. int y = j >= 0 ? num2.charAt(j) - '0' : 0;
    8. int result = x + y + add;
    9. ans.append(result % 10);
    10. add = result / 10;
    11. i--;
    12. j--;
    13. }
    14. // 计算完以后的答案需要翻转过来
    15. ans.reverse();
    16. return ans.toString();
    17. }
    18. }