LeetCode Add Strings
原题链接在这里:https://leetcode.com/problems/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.
题解:
两个string的末位相加放到string builder尾部.
Time Complexity: O(n). n是较长string的length.
Space: O(n).
AC Java:
public class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int carry = 0;
int i = num1.length()-1;
int j = num2.length()-1;
while(i>=0 || j>=0 || carry!=0){
carry += (i<0) ? 0 : num1.charAt(i)-'0';
carry += (j<0) ? 0 : num2.charAt(j)-'0';
sb.insert(0, carry%10);
carry /= 10;
i--;
j--;
}
return sb.toString();
}
}
类似Add Two Numbers II, Add Binary, Plus One.
LeetCode Add Strings的更多相关文章
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of 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”,需要先处理低两位,再 ...
随机推荐
- sql server 对象资源管理器(二)
SQL会缓存大量的数据页面,他还会缓存很多其他信息,包括存储过程的执行计划 ,特定用户的安全上下文等 如果这些信息没有在数据库中缓存,SQL都要重新计算一遍,花额外的时间,所以SQLSERVER对内存 ...
- jQuery- 常规选择器(一)
注意:用size的时候有(),而length没有括号 除了这种方式之外,还可以用转换为 DOM 对象的方式来判断,例如:i$('#pox').get(0) 或 $('#pox')[0] //通过数 ...
- C#写入和读出文本文件
C#写入和读出文本文件 写入文本文件 class WriteTextFile { static void Main() { //如果文件不存在,则创建:存在则覆盖 //该方法写入字符数组换行显示 st ...
- DataSet、DataTable、Json、List 等各种数据的相互转化
1.根据Dataset生成json格式的字符串,不管Dataset里面有多少个表都可以一一生成对应的json字符串,并一次性返回 private string dsToJson(DataSet d ...
- OS X El Capitan的 U 盘制作过程
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 0.在 AppStore 下载 OS X El Capitan AppStore ...
- js汉字与拼音互转终极方案,附简单的JS拼音输入法【转】
github项目地址:https://github.com/liuxianan/pinyinjs 完整demo演示:http://demo.liuxianan.com/pinyinjs/ 汉字转拼音: ...
- ZeroMQ接口函数之 :zmq_msg_init - 初始化一个空的ZMQ消息结构
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_init zmq_msg_init(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ ...
- iOS开发中常见问题集锦
在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...
- js中的数据类型
JS中的数据类型: ——数字 (number)NaN ——字符串(string) ——布尔 (boolean)——函数 (function) 也是对象的一种 ——对象 (object) ...
- Hadoop openssl false
错误如图 检查Hadoop native 经过: 1. 重新编译cdh的hadoop2.5.0,复制native 2. 重新格式化namenode 都不行,另外openssl和openssl-dev都 ...