Source of the topic
https://leetcode.com/problems/multiply-strings/
Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
Test instructions Analysis
Input:two numbers expressed as String
Output:the multiply of the sums
Conditions: The number can be infinitely large, do two number multiplication
such as: "23650379502752" and "865382861454"
Result: "20466633088564555427721408"
Topic ideas
First converts two Str to a list of integers, then takes into account that the number of bits of the product is necessarily less than or equal to Len (str1) +len (str2), first Initializes a list of products of 0, and then follows the rule of multiplying the number of bits.
Attention:
1 The final result requires 0 of the large number to be removed, and if the result is 0 need to return the string "0"
2 Flip: Mul.reverse ()
AC Code (PYTHON)
1_author_ ="YE"2 #-*-coding:utf-8-*-3 4 classsolution (object):5 defMultiply (self, NUM1, num2):6 """7 : Type Num1:str8 : Type Num2:str9 : Rtype:strTen """ OneLen1 =Len (NUM1) ALen2 =Len (num2) - -List1 = [0 forIinchrange (LEN1)] theList2 = [0 forIinchrange (len2)] - - forIinchRange (LEN1): -List1[len1-1-I] =Int (num1[i]) + forIinchRange (len2): -List2[len2-1-I.] =Int (num2[i]) + A #print (LIST1,LIST2) at -Mul = [0 forIinchRange (Len1 +len2)] - - forIinchRange (len2): -carry =0 - forJinchRange (LEN1): inMul[i + j] = Mul[i + j] + carry + (List2[i] * list1[j])% 10 - tocarry = (List2[i] * list1[j])//10 + - ifMul[i + j] >= 10: thecarry = carry + Mul[i + j]//10 *Mul[i + j] = Mul[i + j]% 10 $ Panax Notoginseng - ifCarry >0: theMul[i + len1] + =Carry + ifMul[i + len1] > 10: AMul[i + len1] = mul[i + len1]% 10 theCarry + = Mul[i + len1]//10 + -index = len1 + len2-1 $ whileIndex >=0: $ ifMul[index] >0: - Break -Index-= 1 the - ifIndex + 1 < LEN1 +Len2:WuyiMul[index+1:] = [] the - Mul.reverse () Wu -s ="' About forIinchRange (len (mul)): $s + =str (mul[i]) - ifs = ="': -s ='0' - returns A +STR1 ='12312' theSTR2 ='4234' -s =solution () $ the Print(S.multiply (STR1,STR2))
[Leetcode] (python): 043-multiply Strings