[Leetcode] Multiply Strings

Source: Internet
Author: User

Given numbers represented as strings, return multiplication of the numbers as a string.

Note:the numbers can be arbitrarily large and is non-negative.

Given the number of two string representations, the product of the two numbers is required, and the product is represented by a string. Where the number represented by the string is non-negative.

It's rough, it's not elegant at all Qaq

stringMultiplystringNUM1,stringNUM2) { vector<string>Ans_temp;stringTemp///First calculates the product of the number of NUM1 represented by the number of digits represented by the num2, the product is represented by a string, and is stored in the Ans_temp         for(inti = Num2.length ()-1, Count =0; I >=0; I--, count++) {intup =0;stringTemp for(intj = num1.length ()-1; J >=0; j--) {intLast = ((num2[i)-' 0 ') * (Num1[j]-' 0 ') +up)%Ten; Up = ((Num2[i]-' 0 ') * (Num1[j]-' 0 ') + up)/Ten; Temp.push_back (last +' 0 '); }if(UP) Temp.push_back (up +' 0 ');//Highest bit with roundingReverse (Temp.begin (), Temp.end ()); for(intK = count; K >0; k--) {Temp.push_back (' 0 ');//Low fill 0} reverse (Temp.begin (), Temp.end ());            Ans_temp.push_back (temp); temp =""; }intup =0;stringAnsintt =0;//Sum         for(intj =0; J < Ans_temp.size (); J + +) { for(inti = t; I <ans_temp[j].length (); i++) {intA = up; for(intK = J; K < Ans_temp.size (); k++) {A + = (Ans_temp[k][i]-' 0 '); } up = A/Ten; A = a%Ten; Ans.push_back (A +' 0 ');        } t = Ans_temp[j].length (); }if(UP) {Ans.push_back (up+' 0 '); } reverse (Ans.begin (), Ans.end ());if(ans[0] ==' 0 ')return "0";returnAns }

[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.