LeetCode 415. Add Strings
题目描述:
Given two non-negative numbers
num1andnum2represented as string, return the sum ofnum1andnum2.Note:
- The length of both
num1andnum2is < 5100.- Both
num1andnum2contains only digits0-9.- Both
num1andnum2does not contain any leading zero.- You must not use any built-in BigInteger library or convert the inputs to integer directly.
简单的模拟加法.
class Solution {
public:
string addStrings(string num1, string num2) {
if(num2.length() > num1.length()) swap(num1, num2);
int maxLen = num1.length(), minLen = num2.length(), inc = 0;
string ans(maxLen, '0');
int i;
for(i = 0; i < minLen; i++){
char a = num1[maxLen - i - 1] - '0', b = num2[minLen - i - 1] - '0';
char r = a + b + inc;
if(r >= 10){
inc = 1;
}
else{
inc = 0;
}
ans[maxLen - i - 1] = r % 10 + '0';
}
if(i < maxLen){
for(; i < maxLen; i++){
char r = num1[maxLen - i - 1] - '0' + inc;
if(r >= 10){
inc = 1;
}
else{
inc = 0;
}
ans[maxLen - i - 1] = r % 10 + '0';
}
}
if(inc){
ans.insert(ans.begin(), '1');
}
return ans;
}
};
评论