Leetcode Multiply Strings

Source: Internet
Author: User
Tags multiply strings

Multiply strings original problem of leetcode problem solving

Multiplies the two numbers represented by a string and returns the string result.

Note the point:

    • The given number is a non-negative integer
    • Numbers can be infinity

Example:

Input: NUM1 = "123", num2 = "20"
Output: "2460"

Thinking of solving problems

According to the formula of written calculation multiplication, the multiplication operation is actually the multiplication of each bit first, and then all the results are additive operation. First make clear the number of M-bits multiplied by the number of an n-bit to go to the m+n bit (all 9 times to try). The next 0 equal numbers are aligned at the end of the addition. As 123x456, we can see that 1x6, 2x5, and 3x4 are aligned at the end of the addition, and we can add these numbers to the first round of multiplication, while the next 0 is represented by the position in the list. Then use a loop for carry addition, and finally remove the extra 0 from the beginning. See the following example for specific steps:

123*456 -         -  -          -  3          6[3*6,2*6+3 the,1*6+2 the+3 *,2 *+1 the,1 *,0][ -, -, -, -,4,0][8, -+1, -, -,4,0][8,8, -+2, -,4,0][8,8,0, -+3,4,0][8,8,0,6,5,0]"880650"-"056088""56088"
AC Source
 class solution(object):     def multiply(self, NUM1, num2):        "" : Type Num1:str:type num2:str:rtype:str "" "NUM1 = num1[::-1] Num2 = num2[::-1] Length1 = Len (num1) length2 = Len (num2) temp = [0  for__inchRange (Length1 + length2)]# do multiply         forIinchRange (LENGTH1): forJinchRange (length2): Temp[i + j] + = Int (num1[i]) * INT (num2[j]) carry =0digits = []# do plus         forNuminchTemp:s = carry + num carry = S//TenDigits.append (STR (s%Ten)) result ="". join (digits) [::-1]# Remove The surplus ZeroSub_index =0         forIinchRange (Length1 + length2-1):ifResult[i] = ="0": Sub_index + =1            Else: Breakresult = Result[sub_index:]returnResultif__name__ = ="__main__":assertSolution (). Multiply (" the","20000") ==2400000    assertSolution (). Multiply ("0","3421") ==0

Welcome to my GitHub to get the relevant source code.

Leetcode Multiply Strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.