class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算两个数之和
* @param s string字符串 表示第一个整数
* @param t string字符串 表示第二个整数
* @return string字符串
*/
string solve(string s, string t) {
int len1 = s.size(), len2 = t.size();
if (len1 == 0) {
return t;
}
if(len2 == 0) {
return s;
}
if (s == "0" && t == "0") {
return "0";
}
// 找到第一个非 0 的index
int index1 = 0, index2 = 0;
while(index1 < len1 && (s[index1] == '0')) {
index1++;
}
if (index1 == len1) {
s = "";
} else {
s = s.substr(index1);
}
while(index2 < len2 && (t[index2] == '0')) {
index2++;
}
if (index2 == len2) {
t = "";
} else {
t = t.substr(index2);
}
len1 = s.size();
len2 = t.size();
string ret = "";
int carry = 0;
while(len1 > 0 || len2 >0) {
int val1 = (len1 <= 0) ? 0 : s[len1-1] - '0';
int val2 = (len2 <= 0) ? 0 : t[len2-1] - '0';
int sum = val1 + val2 + carry;
carry = sum / 10;
string tempStr = to_string(sum % 10);
ret += tempStr;
len1--, len2--;
}
if (carry) {
ret += to_string(carry);
}
reverse(ret.begin(), ret.end());
return ret;
// write code here
}
};