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.
Multiplication of large numbers, the use of multiplication rules is good.
1 classSolution {2 Public:3 stringMultiplystringNUM1,stringnum2) {4 if(num1=="0"|| num2=="0")return "0";5 stringresult;6 intlen1=num1.length ();7 intLen2=num2.length ();8 intlen=len1+Len2;9 int* n1=New int[len1];Ten int* n2=New int[len2]; One int* multi=New int[Len]; A for(intI=0; i<len;i++) - { -multi[i]=0; the } - for(intI=0; i<len1;i++) - { -n1[i]=num1[len1-i-1]-'0'; + } - for(intI=0; i<len2;i++) + { An2[i]=num2[len2-i-1]-'0'; at } - for(intI=0; i<len1;i++) - { - for(intj=0; j<len2;j++) - { -multi[i+j]=multi[i+j]+n1[i]*N2[j]; in } - } to for(intI=0; i<len;i++) + { - inttmp=Multi[i]; themulti[i]=tmp%Ten; *multi[i+1]+=tmp/Ten; $ }Panax Notoginseng intFirst_nozero; - for(inti=len-1; i>=0; i--) the { + if(multi[i]!=0) A { thefirst_nozero=i; + Break; - } $ } $ for(inti=first_nozero;i>=0; i--) - { -result+=Char(multi[i]+'0'); the } - Delete[]n1;Wuyi Delete[]n2; the Delete[]multi; - Wu returnresult; - } About};
[Leetcode] Multiply Strings