Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page
  • 1.Description(Easy)
  • Notice
  • 2.Code

Was this helpful?

  1. Onsite I

655.Big Integer Addition

Previous88.Lowest Common AncestorNext221.Add Two Numbers II

Last updated 5 years ago

Was this helpful?

1.Description(Easy)

Given two non-negative integersnum1andnum2represented as string, return the sum ofnum1andnum2.

Notice

  • 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.

Example

Given num1 ="123", num2 ="45" return"168"

2.Code

乘法的答案在这里:

跟167 Add two number基本一样,需要一个carry控制进位。

需要注意的是把char转换成int的时候要用num.charAt(i)-'0',而不能直接强制转换(int).

public String addStrings(String num1, String num2) {
        if((num1==null ||num1.length()==0)&& (num2==null || num2.length()==0)){
            return "";
        }
        if(num1==null ||num1.length()==0){
            return num2;
        }
        if(num2==null || num2.length()==0){
            return num1;
        }

        int carry=0;
        StringBuilder result=new StringBuilder();
        int i=num1.length()-1;
        int j=num2.length()-1;
        while(i>=0 && j>=0){
            int sum=num1.charAt(i)-'0'+num2.charAt(j)-'0'+carry;
            carry=sum/10;
            int current=sum%10;
            result.append(current);
            i--;
            j--;
        }
        while(i>=0){
            int sum=num1.charAt(i)-'0'+carry;
            carry=sum/10;
            int current=sum%10;
            result.append(current);
            i--;
        }
        while(j>=0){
            int sum=num2.charAt(j)-'0'+carry;
            carry=sum/10;
            int current=sum%10;
            result.append(current);
            j--;
        }
        if(carry!=0){
            result.append(carry);
        }

        String res=result.reverse().toString();
        return res;

    }
Tags
Mathematics
Airbnb
Google
http://www.jiuzhang.com/solutions/big-integer-multiplication/