LeetCode415. Add Strings小学生都会
来源:互联网 发布:javascript与html论文 编辑:程序博客网 时间:2024/04/30 08:44
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does 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) {
int eb = 0;
int len = max(num1.size(), num2.size());
vector<char> vc;
char a, b;
for (int i = 0; i < len; i++)
{
if (i < num1.size()) {
a = num1[num1.size()-1-i];
} else {
a = '0';
}
if (i < num2.size()) {
b = num2[num2.size()-1-i];
} else {
b = '0';
}
int val = a - '0' + b - '0' + eb;
eb = val / 10;
char c = '0' + val % 10;
vc.push_back(c);
}
if (eb > 0) {
vc.push_back('0' + eb);
}
string result(vc.rbegin(), vc.rend());
return result;
}
};
- LeetCode415. Add Strings小学生都会
- Leetcode415. Add Strings
- Add Strings
- Add Strings
- Add Strings
- Add Strings
- Add Strings
- Add Strings
- Add Strings
- 415. Add Strings
- Leetcode 415 Add Strings
- Leetcode-415. Add Strings
- 415. Add Strings
- leetcode_415 Add Strings
- 415 add strings
- LeetCode[415] Add Strings
- 415. Add Strings
- LeetCode #415: Add Strings
- H5学习之路-手机短信验证码的实现
- 面试题8:旋转数组的最小数字
- Swagger与SpringMVC项目整合
- 六、原型模式(Prototype)
- 二叉树
- LeetCode415. Add Strings小学生都会
- Myeclipse调错:The type java.util.Map$Entry cannot be resolved.
- 2017上半年规划
- mybatis入门-mapper代理原理
- 《JQuery之系统化学习》
- RecyclerView使用攻略(刷新篇)
- mybatis学习笔记-实现查询用户信息功能
- Tensorflow学习精要版IV ---- 开始稍微深入了解一下
- 面试题9:菲波那切数列