[LeetCode]415. Add Strings
来源:互联网 发布:手机淘宝店铺链接在哪 编辑:程序博客网 时间:2024/06/02 06:15
[LeetCode]415. Add Strings
题目描述
思路
水题,遍历字符串,按位相加即可
代码
#include <iostream>#include <string>using namespace std;class Solution {public: string addStrings(string num1, string num2) { string res; int temp1, temp2, carry = 0; for (int i = num1.size() - 1, j = num2.size() - 1; ; --i, --j) { if (i < 0 && j < 0) break; temp1 = 0, temp2 = 0; if (i >= 0) temp1 = num1[i] - '0'; if (j >= 0) temp2 = num2[j] - '0'; int temp_res = (temp1 + temp2 + carry) % 10; carry = (temp1 + temp2 + carry) / 10; res = to_string(temp_res) + res; } if (carry) res = to_string(carry) + res; return res; }};int main() { Solution s; cout << s.addStrings("11", "99") << endl; system("pause"); return 0;}
0 0
- Leetcode-415. Add Strings
- LeetCode 415. Add Strings
- [LeetCode]--415. Add Strings
- LeetCode 415. Add Strings
- LeetCode 415. Add Strings
- LeetCode 415. Add Strings
- leetcode 415. Add Strings
- 【LeetCode】 415. Add Strings
- LeetCode 415. Add Strings
- Leetcode 415. Add Strings
- LeetCode - 415. Add Strings
- LeetCode 415. Add Strings
- Leetcode 415. Add Strings
- Leetcode-415. Add Strings
- LeetCode-415. Add Strings
- leetcode 415. Add Strings
- [leetcode]415. Add Strings
- 【Leetcode】415. Add Strings
- 那些年踩过的坑之:first-child伪类选择器
- dAO模式理解2
- jenkines配置
- 最简单的基于FFmpeg的移动端例子:IOS HelloWorld
- AngularJS中的显示隐藏示例
- [LeetCode]415. Add Strings
- 用Hive分析Nginx日志
- mysql 存储过程+mybatis
- [Python学习] python 科学计算库NumPy—tile函数用法
- JS实现选择器全选、反选
- JConsole使用
- MyEclipse2014修改Web-root folder的值
- LeetCode11-Container With Most Water
- ubuntu16.04 查看内核,升级内核,删除内核,切换内核