首页 > 代码库 > Multiply Strings

Multiply Strings

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

Note: The numbers can be arbitrarily large and are non-negative.

 

主要看如何一次无bug。

 1 class Solution { 2 public: 3     string multiply(string num1, string num2) { 4         if(num1.size()==0||num2.size()==0) return "0"; 5         int len1=num1.size(); 6         int len2=num2.size(); 7         int carry=0; 8         string pre; 9         for(int i=len1-1;i>=0;i--)10         {11             int a=num1[i]-0;12             string cur;13             int weishu=len1-i;14             while(weishu>1)15             {16             cur.append(1,0);17             weishu--;18             }19             for(int j=len2-1;j>=0;j--)20             {21                 int b=num2[j]-0;22                 int t=(a*b+carry)%10;23                 carry=(a*b+carry)/10;24                 cur.append(1,(t+0));25             }26             if(carry!=0)27             {28                 cur.append(1,(carry+0));29                 carry=0;30             }31             sumstr(pre,cur);32             pre=cur;33             34         }35         std::reverse(pre.begin(),pre.end());36         int start=0;37         while(pre[start]==0&&start<pre.size())38         {39             start++;40         }41         if(start==pre.size())return "0";42         return pre.substr(start,pre.size()-start);43     }44     45     void sumstr(string& pre, string& cur)46     {47         int len1=pre.size();48         int len2=cur.size();//should be equal or longer than len149         int carry=0;50         int i=0;51         while(i<len1)52         {53             int t=(pre[i]-0+cur[i]-0+carry)%10;54             carry=(pre[i]-0+cur[i]-0+carry)/10;55             cur[i]=t+0;56             i++;57             58         }59         while(carry!=0&&i<len2)60         {61             int t=(cur[i]-0+carry)%10;62             carry=(cur[i]-0+carry)/10;63             cur[i]=t+0;64             i++;65         }66         if(carry!=0)cur.append(1,(carry+0));67         68     }69 };
1:    string multiply(string num1, string num2) {  2:      // Start typing your C/C++ solution below  3:      // DO NOT write int main() function  4:      if(num1.size() ==0 || num2.size() ==0) return 0;  5:      string res(num1.size()+num2.size()+1, 0);  6:      std::reverse(num1.begin(), num1.end());  7:      std::reverse(num2.begin(), num2.end());  8:      for(int i =0; i < num1.size(); i++)  9:      {  10:        int dig1 = num1[i] -0;  11:        int carry = 0;  12:        for(int j = 0; j< num2.size(); j++)  13:        {  14:          int dig2 = num2[j] - 0;  15:          int exist = res[i+j] -0;          16:          res[i+j] = (dig1*dig2+carry+ exist) % 10 +0;    17:          carry = (dig1*dig2+carry+exist)/10;  18:        }  19:        if(carry >0)  20:        {  21:          res[i+num2.size()] = carry + 0;  22:        }  23:      }  24:      std::reverse(res.begin(), res.end());  25:      int start =0;  26:      while(res[start] ==0 && start < res.size())  27:      {  28:        start++;  29:      }  30:      if(start == res.size()) return "0";  31:      return res.substr(start, res.size()-start);  32:    }