[LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - 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 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] Add Strings 字符串相加的更多相关文章
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [leetcode]415. Add Strings字符串相加
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 415 Add Strings 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和 ...
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2 ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 力扣(LeetCode)415. 字符串相加
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...
随机推荐
- 读书笔记--SQL必知必会20--管理事务处理
20.1 事务处理 使用事务处理(transaction processing),通过确保成批的SQL操作要么完全执行,要么完全不执行,来维护数据库的完整性. 如果没有错误发生,整组语句提交给数据库表 ...
- .Net 序列化(去除默认命名空间,添加编码)
1.序列化注意事项 (1).Net 序列化是基于对象的.所以只有实例字段呗序列化.静态字段不在序列化之中. (2)枚举永远是可序列化的. 2.XML序列化时去除默认命名空间xmlns:xsd和xmln ...
- php内核分析(四)-do_cli
这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux # main 把剩下的代码增加了下注释全部贴出来了(这个是简化后的main函数,去掉了一些无关紧要的代码段): int m ...
- passport源码研究
passport的验证过程主要依赖具体的验证策略来实现的,比较常用的有session策略.local策略和github策略等,验证逻辑都是在这些策略类中定义的.passport模块的定 ...
- Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App
安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...
- 报错:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
SqlParameter[] sp = { new SqlParameter("@nGridID",SqlDbType.BigInt), new SqlParameter(&quo ...
- C++四种类型转换方式。
类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...
- 第一篇 Entity Framework Plus 之 Audit
一般系统会有登陆日志,操作日志,异常日志,已经满足大部分的需求了.但是有时候,还是需要Audit 审计日志,审计日志,主要针对数据增,改,删操作数据变化的记录,主要是对数据变化的一个追踪过程.其中主要 ...
- JDBC_part3_批处理_事务_元数据
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! JDBC_day03 String a = " ...
- canvas调用scale或者drawImage图片操作后,锯齿感很明显的解决
<script type="text/javascript"> //解决canvas画画图片 var mengvalue = -1; var phoneWidth = ...