43.Multiply Strings

Source: Internet
Author: User
Tags 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.

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

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.