//给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。 
//
// 
//
// 提示: 
//
// 
// num1 和num2 的长度都小于 5100 
// num1 和num2 都只包含数字 0-9 
// num1 和num2 都不包含任何前导零 
// 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式 
// 
// Related Topics 字符串 
// 👍 312 👎 0

/*
* 415 字符串相加
* 2021-02-23 22:36:11
* @author oxygenbytes
*/ 
#include "leetcode.h" 
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    string addStrings(string num1, string num2) {
        string result;

        int i = num1.size() - 1;
        int j = num2.size() - 1;
        int carry = 0;
        while(i >= 0 || j >= 0 || carry != 0){
            int x = i >= 0 ? num1[i] - '0' : 0;
            int y = j >= 0 ? num2[j] - '0' : 0;

            int cur = (x + y + carry) % 10;
            carry = (x + y + carry) / 10;
            result.push_back('0' + cur);
            i--;
            j--;
        }
        reverse(result.begin(), result.end());
        return result;
    }
};
//leetcode submit region end(Prohibit modification and deletion)