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”,需要先处理低两位,再 ...
随机推荐
- NHibernate VS IbatisNet
NHibernate 是当前最流行的 Java O/R mapping 框架Hibernate 的移植版本,当前版本是 1.0 .2 .它出身于sf.net..IbatisNet 是另外一种优秀的 ...
- maven 的构建异常 Could not find artifact ... and 'parent.relativePath'
完整的异常提示: Non-resolvable parent POM: Could not find artifact com.ecp:ecp-main:pom:0.0.1-SNAPSHOT and ...
- Leetcode-Populating Next Right Pointer in Binary Tree II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 『浅入浅出』MySQL 和 InnoDB
作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...
- 巨蟒python全栈开发-第13天 内置函数 匿名函数lambda
一.今日内容总览 1.内置函数(1):并不是每一个内置函数都是那么常用 上菜:内置函数部分//思维导图:https://www.processon.com/view/link/5b4ee15be4b0 ...
- Powershell About File System
File System Rights Get-Acl $sharepath | select -expand access | where { !$_.IsInherited -AND $_.file ...
- ubuntu/debian gpg error no_pubkey 解决方法
GPG error: http://ppa.launchpad.net precise Release: The following signatures couldn’t be verified b ...
- SpringMVC 之 RESTful 风格的增删改查
1. 视图和视图解析器 视图解析器 请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View 或 ModelMap 等类型的处理方法, SpringMVC 也会 ...
- make Makefile 与 cmake CMakeLists.txt
make Makefile 与 cmake CMakeLists.txt 大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器 ...
- Django--Web应用介绍/http协议