原题链接在这里: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:

  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.

题解:

两个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();
}
}

跟上Multiply Strings.

类似Add Two Numbers IIAdd BinaryPlus One.

LeetCode Add Strings的更多相关文章

  1. LeetCode——Add Strings

    LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...

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

    Given two non-negative numbers num1 and num2 represented as string, return the sum of 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. C++静态库与动态库

    C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...

  2. VS2012 自动为类文件添加头注释

    1. 打开文件夹 %VS110COMNTOOLS%..\IDE\ItemTemplates\CSharp\Code\1033\Class 2. 打开 Class.cs 文件,在其中添加 public ...

  3. java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法

    1 BitmapFactory.decodeFile(imageFile); 用BitmapFactory解码一张图片时,有时会遇到该错误.这往往是由于图片过大造成的.要想正常使用,则需要分配更少的内 ...

  4. dp表模型-如何写出for循环动态规划

    题目很肤浅.. 但是这件事我们要做.. 那么有一种方法叫做刷表法.. 当你发现这个问题具有最优子结构,重叠子问题时 那么这是一个dp问题是使用本方法的前提 画出该dp状态所对应的矩阵 画出转移关系线. ...

  5. Monitoring Processes with Supervisord

    If you're interested in more of this type of content, check out the Servers for Hackers eBook! As so ...

  6. jQuery- 常规选择器(一)

    注意:用size的时候有(),而length没有括号 除了这种方式之外,还可以用转换为 DOM 对象的方式来判断,例如:i$('#pox').get(0) 或  $('#pox')[0]  //通过数 ...

  7. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  8. Difinition Of Done

    A Story is Sprint ready (Rally Defined) when............. The story has well defined and testable ac ...

  9. Media Formatters in ASP.NET Web API 2

    原文:http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters 1. 网络媒体类型 媒体类型,也叫作 ...

  10. win下sass安装失败的一种可能

    首先声明,本篇转自CSDN的LZGS_4. 今天我也遇到这个问题,就把文章擅自转载了,方便自己也方便更多的人吧. 因为sass和compass依赖于ruby环境,所以装之前先确认装了ruby.可到官网 ...