Leetcode 415. Add Strings (Easy) (cpp)
来源:互联网 发布:云计算java工程师招聘 编辑:程序博客网 时间:2024/05/16 19:00
Leetcode 415. Add Strings (Easy) (cpp)
Tag: Math
Difficulty: Easy
/*415. Add Strings (Easy)Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is < 5100.Both num1 and num2 contains only digits 0-9.Both num1 and num2 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) { string res; int i = num1.length() - 1, j = num2.length() - 1; long carry = 0; while ( i >= 0 || j >= 0 || carry > 0) { if (i >= 0) { carry = (num1[i--] - '0') + carry; } if (j >= 0) { carry = (num2[j--] - '0') + carry; } res += to_string(carry % 10); carry /= 10; } reverse(res.begin(), res.end()); return res; }};
0 0
- Leetcode 415. Add Strings (Easy) (cpp)
- Leetcode 415. Add Strings[easy]
- Leetcode 205. Isomorphic Strings (Easy) (cpp)
- Leetcode 258. Add Digits (Easy) (cpp)
- Leetcode 67. Add Binary (Easy) (cpp)
- 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
- poj算法题1061——青蛙的约会
- 简单算法之素数筛法
- android中使用自定义字体以及图标字体
- Caffe学习系列(2):数据层及参数
- HDU2149:Public Sale(巴什博弈)
- Leetcode 415. Add Strings (Easy) (cpp)
- 12306泄露数据可视化分析
- bootstrap Less
- js 面试的坑(四)
- 上帝视角——我是一个线程
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者
- JavaScriptES6语法小结
- 通过Intent传递对象的另类方法
- genymotion 日志文件报错 object is not found