题意:大数加法。
思路:简单模拟。
class Solution {
public:
string addStrings(string num1, string num2) {
string re;
int c = 0;
std::reverse(num1.begin(), num1.end());
std::reverse(num2.begin(), num2.end());
int i = 0;
while(i < num1.length() ||i < num2.length()) {
int a = 0;
int b = 0;
if(i < num1.length()) a = num1[i] - '0';
if(i < num2.length()) b = num2[i] - '0';
re += (a + b + c) % 10 + '0';
c = (a + b + c) / 10;
i ++;
}
if(c) re += c + '0';
std::reverse(re.begin(), re.end());
return re;
}
};

本文介绍了一种使用C++实现的大数加法算法。通过字符串逆序处理,逐位相加并进位的方式,有效地解决了大数运算的问题。
387

被折叠的 条评论
为什么被折叠?



