一、题目
二、代码
class Solution {
public:
string addStrings(string num1, string num2) {
int end1 = num1.size()-1;
int end2 = num2.size()-1;
int next=0;//进位
string s;
while(end1>=0 || end2>=0)
{
int x1 = end1>=0?num1[end1]-'0':0;
int x2 = end2>=0?num2[end2]-'0':0;
int ret = x1 + x2 +next;
next = ret/10;
int y = ret%10;
s.insert(0,1,'0'+y);
end1--;
end2--;
}
if(next == 1)//像‘9’+‘1’这种情况,剩下一个进位
{
s.insert(0,1,'0'+next);
}
return s;
}
};