【问题描述】
给定两个字符串形式的非负整数 n1 和n2 ,计算它们的和。
注意:
n1 和n2 的长度都小于 5 1 0 0 .
n1 和n2 都只包含数字 0 - 9 . n1 和n2 都不包含任何前导零。
你不能使用任何內建 Bi g I n t e g e r 库, 也不能直接将输入的字符串转换为整数形式。
示例1:
"12345678901278"+"234"="12345678901512"
【问题分析】
我们先来看看两数相加的算数竖式:
由图可以看出,两个字符串相加是从右向左一一对应相加的。如下图:
int i = n1.size() - 1, j = n2.size() - 1;
int x = n1.at(i) - '0';
int y = n2.at(j) - '0';
int sum = x + y;
但是上面只是个位相加。字符串一个位置只能存一个个位数,那么如果出现了十位数呢?所以我们加入一个变量carry 用来进位。当x+y得到sum后,carry存储 sum 的 sum/10 的结果,不论sum是十位数还是个位数。然后对应的字符串位置存sum%10的结果。看代码:
int i = n1.size() - 1, j = n2.size() - 1,carry=0;
int x = n1.at(i) - '0';
int y = n2.at(j) - '0';
int sum = x + y + carry;
s += (sum % 10) + '0';//添加到字符串尾部
carry = sum / 10;
看最终代码:
string addStrings(string n1, string n2)
{
string s;
int i = n1.length() - 1, j = n2.length() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry != 0) {
int x = i < 0 ? 0 : n1.at(i--) - '0';
int y = j < 0 ? 0 : n2.at(j--) - '0';
int sum = x + y + carry;
s += (sum % 10) + '0';//添加到字符串尾部
carry = sum / 10;
}
reverse(s.begin(), s.end());
return s;//对字符串反转
}
总结:
这题非常简单,解题思路大同小异,都是先从两个字符串的最后一位开始相加,只不过 写的时候会有多种方式。
比如可以用栈:
string addStrings(string n1, string n2)
{
string s;
stack<char> st;
int i = n1.length() - 1, j = n2.length() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry != 0) {
carry += i < 0 ? 0 : n1.at(i--) - '0';
carry += j < 0 ? 0 : n2.at(j--) - '0';
st.push((carry % 10) + '0');
carry = carry / 10;
}
while (!st.empty())
{
s += st.top();
st.pop();
}
return s;
}
或者直接用insert ,那么就不需要反转了:
string addStrings(string n1, string n2)
{
string s;
int i = n1.length() - 1, j = n2.length() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry != 0) {
int x = i < 0 ? 0 : n1.at(i--) - '0';
int y = j < 0 ? 0 : n2.at(j--) - '0';
int sum = x + y + carry;
s.insert(0,1,((sum % 10) + '0'));
carry = sum / 10;
}
return s;
}
甚至还可以用递归写,这里我就不写了,感兴趣的可以自己尝试。