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的更多相关文章

  1. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  2. LeetCode Add Strings

    原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2  ...

  3. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  4. 36. leetcode 415. Add Strings

    415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum ...

  5. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  6. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. LeetCode_415. Add Strings

    415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...

  9. 447. Add Strings

    原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...

随机推荐

  1. 深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用

    SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保持一致 ...

  2. shop++源码反编译----随笔

    一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...

  3. getComputedStyle获取css属性与IE下的currentStyle获取到的值不同

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  4. Ubuntu 16.04 安装google浏览器

    因为安装的Linux是64位的Ubuntu 16.04系统,所以本人决定也安装64位的谷歌Chrome浏览器.在 Ubuntu 16.04 中,要想使用谷歌的 Chrome 浏览器,可以通过命令行的方 ...

  5. cross-origin HTTP request

    w https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS A resource makes a cross-ori ...

  6. root Permission denied

    w 遇见现象,原因待查

  7. delphi中String 和 动态静态数组

    默认string类型为ansiString:有编译开关控制 shortString: strShort : shortString; strShort 大小256字节,可根据sizeof()计算出,s ...

  8. java 日期的格式化 输入/输出

    想要得到形如2018.07.09的格式化好的当天日期 创建Date对象,调用SimpleDateFormat对象的format方法: indexstr="logstash-"+ne ...

  9. Python(数据库之约束表的关系)

    一.约束 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性 主要分为: RIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  10. mysql聚合函数操作

    1.mysql对中文进行排序 注:是用convert函数用gb2312编码转换 SELECT * FROM 表名 ORDER BY CONVERT(字段名 USING gb2312 ) ASC;