[leetcode]415. Add Strings
Analysis
everyday is the first day of your rest life—— [间歇性迷茫+1~]
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
其实就是大数相加啦~
Implement
class Solution {
public:
string addStrings(string num1, string num2) {
int len1 = num1.size();
int len2 = num2.size();
len1 -= 1;
len2 -= 1;
int c = 0;
string res = "";
while(len1>=0 || len2>=0){
if(len1 >= 0){
c += num1[len1]-'0';
len1--;
}
if(len2 >= 0){
c += num2[len2]-'0';
len2--;
}
res += (c%10+'0');
c /= 10;
}
if(c > 0)
res += (c+'0');
reverse(res.begin(), res.end());
return res;
}
};