leetcode 415. Add Strings 字符串加法
来源:互联网 发布:fcc 网络中立 编辑:程序博客网 时间:2024/05/22 08:20
Given two non-negative integers 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.
下面是字符串的加法,很简单,直接看代码
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <bitset>using namespace std;class Solution {public: string addStrings(string num1, string num2) { string res = ""; int i = num1.length() - 1, j = num2.length() - 1; int jinwei = 0; while (i >= 0 || j >= 0) { int a = i >= 0 ? num1[i] - '0' : 0; int b = j >= 0 ? num2[j] - '0' : 0; res = to_string((a + b + jinwei) % 10) + res; jinwei = (a + b + jinwei) / 10; i--; j--; } if(jinwei>0) res = to_string(jinwei) + res; return res; }};
阅读全文
0 0
- leetcode 415. Add Strings 字符串加法
- Leetcode 415. Add Strings 字符串加法 解题报告
- LeetCode 415. Add Strings (大数加法,字符串处理)
- 【LeetCode】415. Add Strings,用string模拟加法
- Leetcode#415. Add Strings (大数加法超简单写法)
- LeetCode 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
- 模板
- 重复造轮子-C语言风格string库函数的实现
- 1407 用两种方法对IPV6地址进行压缩的c程序实现
- C语言动态数组建立方法
- 阿里巴巴Java开发手册学习小结3-OOP规约
- leetcode 415. Add Strings 字符串加法
- 求助!!!!急!!!
- Tomcat 日志分割.
- Tag文件的创建与应用
- SSM 框架 Maven项目整合实例
- git 命令--上传代码 && Window系统下搭建GIT本地服务器
- 自动化运维工具ansible的安装与使用
- foreach的continue和break : Uncaught SyntaxError: Illegal continue statement
- pygame库写游戏——入门<5>——游戏中的视觉