Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
Solution: Direct multiplication will overflow, need a multiply.
Use an array of d[] to load all the multiples.
Correct algorithm:
Public classSolution { Publicstring Multiply (string num1, String num2) {string S1=NewStringBuilder (NUM1). Reverse (). toString (); String S2=NewStringBuilder (num2). Reverse (). toString (); int[] d=New int[S1.length () +s2.length ()]; for(intI=0;i<s1.length (); i++){ for(intJ=0;j<s2.length (); j + +) {D[i+j]=d[i+j]+ (S1.charat (i)-' 0 ') * (S2.charat (j)-' 0 '); }} StringBuilder SB=NewStringBuilder (); intcarry; intDigit; for(inttemp=0;temp<d.length;temp++) {digit=d[temp]%10; Carry=d[temp]/10; Sb.insert (0, digit); if(temp<d.length-1) D[temp+1]=d[temp+1]+carry; } while(Sb.length () >0 && sb.charat (0) = = ' 0 ') {Sb.deletecharat (0); } if(Sb.length () ==0){ return"0"; } returnsb.tostring (); }}
43.Multiply Strings