``````class Solution {
public:
string addStrings(string num1, string num2) {
int m = num1.size(),n = num2.size();
int carry = 0;
string res;

while(m > 0 && n > 0)
{
int num = num1[m-1]-'0'+num2[n-1]-'0'+carry;
carry = 0;
if(num >= 10)
{
carry = 1;
num = num%10;
}
res.insert(res.begin(),'0'+num);
m--;
n--;
}

while(n > 0)
{
int num = num2[n-1]-'0'+carry;
carry = 0;
if(num >= 10)
{
carry = 1;
num = num%10;
}
res.insert(res.begin(),'0'+num);
n--;
}

while(m > 0)
{
int num = num1[m-1]-'0'+carry;
carry = 0;
if(num >= 10)
{
carry = 1;
num = num%10;
}
res.insert(res.begin(),'0'+num);
m--;
}

if(carry)
res.insert(res.begin(),'1');

return res;
}
};``````

``````执行结果：通过 显示详情

``````class Solution {
public:
string addStrings(string num1, string num2) {
int m = num1.size() - 1, n = num2.size() -1;
int carry = 0;
string res;

while(m >= 0 || n >= 0 || carry)
{
int sum = carry;
if(m >= 0) sum += num1[m--]-'0';
if(n >= 0) sum += num2[n--]-'0';

carry = sum/10;
sum = sum %10;
res += sum +'0';
}
reverse(res.begin(),res.end());
return res;
}
};``````