LeetCode——Add Strings
LeetCode——Add Strings
Question
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.
解题思路
这道题就是所谓的大整数加法操作,因为超过了计算机所能表示的范围数,所以用字符串表示,然后注意到就是,当两个长度不一样的时候,可以考虑往一个较短的字符串补‘0’,让两个字符串的长度保持一致。
具体实现
class Solution {
public:
// 大整数加法
string addStrings(string num1, string num2) {
string n1(num1.rbegin(), num1.rend());
string n2(num2.rbegin(), num2.rend());
int size1 = n1.length();
int size2 = n2.length();
if (size1 < size2) {
for (int i = 0; i < size2 - size1; i++) {
n1 += '0';
}
}
if (size1 > size2) {
for (int j = 0; j < size1 - size2; j++) {
n2 += '0';
}
}
int remainer = 0;
string str;
for (int i = 0; i < n1.length(); i++) {
int sum = n1[i] - 48 + n2[i] - 48 + remainer;
remainer = 0;
if (sum >= 10) {
sum = sum - 10;
remainer = 1;
}
char tmp = sum + 48;
str += tmp;
}
if (remainer == 1)
str += '1';
string str1(str.rbegin(), str.rend());
return str1;
}
};
LeetCode——Add Strings的更多相关文章
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2 ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 36. leetcode 415. Add Strings
415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode_415. Add Strings
415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...
- 447. Add Strings
原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...
随机推荐
- Delphi TreeView – 自动给标题上加图片
Delphi TreeView – 自动给标题上加图片 当处理完TreeView控件树形结构的数据后,根据不同的树形节点Level,加上不同的图片. 图片的ImageList已经放置好,并且TreeV ...
- 【BZOJ4896】[Thu Summer Camp2016]补退选 Trie树
[BZOJ4896][Thu Summer Camp2016]补退选 Description X是T大的一名老师,每年他都要教授许多学生基础的C++知识.在T大,每个学生在每学期的开学前都需要选课,每 ...
- 创建Json
1)生成 JSON: 方法 1.创建一个 map,通过构造方法将 map 转换成 json 对象 Map<String, Object> map = new HashMap<Stri ...
- HDU 3578 Greedy Tino(双塔DP)
Greedy Tino Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Windows Server 2008及以上系统磁盘无法查看(About UAC and ACE)
在windows Server2008及以上系統,如果UAC Enabled,ACE列表中不會包含Administrators成員的SID,所以即使你是administrators的成員,也無法訪問D ...
- Java实现对List去重
方式一,使用for循环遍历去除List中的重复元素代码如下 public static void main(String[] args) { Test07 test07 = new Test07(); ...
- pycharm调试scrapy
pycharm调试scrapy 创建一个run.py文件作为调试入口 run.py中,name是要调试的爬虫的名字(注意,是爬虫类中的name,而不是爬虫类所在文件的名字) 拼接爬虫运行的命令,然后用 ...
- 【POJ3615】Cow Hurdles 最短路,你若LCA,我仍不拦你。
NOIP2013货车运输.仅仅只是数据范围小了很多. 不到150s打完而且AC. . 额.当然.我写的是Floyd. 写LCA的真过分. #include <cstdio> #includ ...
- Android View体系 系列文章
http://www.cnblogs.com/Free-Thinker/p/6768783.html
- redis的安装与配置(一)
1. 介绍 Redis is an open source (BSD licensed), in-memory data structure store, used as database, cach ...