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

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

这道题让我们求两个字符串的相加,之前 LeetCode 出过几道类似的题目,比如二进制数相加,还有链表相加,或是字符串加1,基本思路很类似,都是一位一位相加,然后算和算进位,最后根据进位情况看需不需要补一个高位,难度不大,参见代码如下:

class Solution {
public:
string addStrings(string num1, string num2) {
string res = "";
int m = num1.size(), n = num2.size(), i = m - , j = n - , carry = ;
while (i >= || j >= ) {
int a = i >= ? num1[i--] - '' : ;
int b = j >= ? num2[j--] - '' : ;
int sum = a + b + carry;
res.insert(res.begin(), sum % + '');
carry = sum / ;
}
return carry ? "" + res : res;
}
};

讨论:由热心网友 zzcRq1 提供了一种 Follow up,当字符串中有小数点和负号怎么处理。博主稍微想了一下,感觉还挺麻烦的,首先应该判断有几个负号,若只有一个,则是减法,而若负号的个数是0个或者是2个的时候,则还是加法。而小数点的处理就是将小数部分和整数部分拆分出来,分别进行加法和减法,最后再拼接上去,感觉大概应该是这样处理的,感兴趣的童鞋可以写个代码实现一样,可以在评论区贴上你的代码哈~

Github 同步地址:

https://github.com/grandyang/leetcode/issues/415

类似题目:

Add Digits

Add Binary

Add Two Numbers

Multiply Strings

Add to Array-Form of Integer

参考资料:

https://leetcode.com/problems/add-strings/

https://leetcode.com/problems/add-strings/discuss/90453/C%2B%2B_Accepted_13ms

https://leetcode.com/problems/add-strings/discuss/90436/Straightforward-Java-8-main-lines-25ms

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 415. Add Strings 字符串相加的更多相关文章

  1. [leetcode]415. Add Strings字符串相加

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

  2. 415 Add Strings 字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意:    num1 和num2 的长度都小于 5100.    num1 和num2 都只包含数字 0-9.    num1 和 ...

  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] Add Strings 字符串相加

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

  6. LeetCode - 415. Add Strings

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

  7. leetcode 415 两个字符串相加

    string addstring(string s1,string s2) { string ans=""; ; ,j=s2.length()-;i>=||j>=;i- ...

  8. 【leetcode】415. Add Strings

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

  9. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

随机推荐

  1. 普通的行专列;oracle行专列;更新中。。。

    题记 本来想写一个完整的表创建,但是其他人都写过啦,要不这样,你们有什么行转列的问题给我留言,我直接回答如何 Oracle的行转列 这篇文章不错:https://blog.csdn.net/huay_ ...

  2. pytorch_13_pytorch 中tensor,numpy,PIL的转换

    PIL:使用Python自带图像处理库读取出来的图片格式numpy:使用Python-opencv库读取出来的图片格式tensor:pytorch中训练时所采取的向量格式 import torch i ...

  3. 浅谈javascript引擎执行代码的步骤-(2019-2)

    平时面试经常会遇到类似下面的这种题,而且千变万化,让人一个头两个.示例这道题算是稍微有点难度的了,这种题考查的是JavaScript引擎执行代码的步骤. b = 'cba'; function a(a ...

  4. Elastic Beats介绍

    需要学习的地方:概念,用法,模块使用 Elastic Beats介绍 Elastic Stack传统上由三个主要组件(Elasticsearch,Logstash和Kibana)组成,早已脱离了这种组 ...

  5. mac中使用expect脚本,让iTerms保存密码登录ssh

    最近工作中需要使用ssh连接到centos服务器中,以前公司都是直接配的私钥就可以免密登录了.这里还用的密码. 由于,我一直用的是iTerm2,所以在网上搜索了下,找到了一种方案,那就是expect脚 ...

  6. JZOJ.2117. 【2016-12-30普及组模拟】台风

    题目大意: 天气预报频道每天从卫星上接受卫星云图.图片被看作是一个矩阵,每个位置上要么是”#”,要么”.”,”#”表示该位置没有云,”.”表示有云,地图上每个位置有多达8个相邻位置,分别是,左上.上. ...

  7. [PHP] 使用ftell和fseek函数直接定位文件位置获取部分数据

    对于大文件只获取部分数据很有用 1.使用ftell函数可以获取当前指针的字节位置2.使用fseek函数可以直接定位到指定的位置3.读取指定字节的数据就可以部分获取文件内容了 <?php clas ...

  8. 网络远程唤醒 WOL Magic Packet【转】

    转自:https://www.cnblogs.com/dcb3688/p/4608063.html Magic Packet Magic Packet白皮书介绍: The basic technica ...

  9. 构建Apache Web服务器

    Apache 是世界使用排名第一的 Web 服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一.Apache工作模式有多种,其 ...

  10. 爬虫---Beautiful Soup 反反爬虫事例

    前两章简单的讲了Beautiful Soup的用法,在爬虫的过程中相信都遇到过一些反爬虫,如何跳过这些反爬虫呢?今天通过知乎网写一个简单的反爬中 什么是反爬虫 简单的说就是使用任何技术手段,阻止别人批 ...