加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

leetcode || 43、Multiply Strings

发布时间:2020-12-14 02:41:21 所属栏目:大数据 来源:网络整理
导读:problem: 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. Hide Tags ? Math?String 将两个用字符串表示的大数相乘 thinking: (1)用字

problem:

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.

Hide Tags
? Math?String
将两个用字符串表示的大数相乘

thinking:

(1)用字符串表示大数相乘,可以不用考虑溢出的问题

(2)第一种方法比较笨重,直接模拟乘法(我在自己机器上测试没问题,提交时显示runtime error,郁闷)

class Solution {
public:
    string multiply(string num1,string num2) {
        vector<string> container;
        string ret;
        int length1=num1.size();
        int length2=num2.size();
        if(length2>length1)
        {
            string tmp=num1;
            num1=num2;
            num2=tmp;
        }

        int i=0;
        int index=length2-1;
        while(index>=0)
        {
            string res=string_multiply_char(num1,num2.at(index),i);
            container.push_back(res);
            index-- ;
            i++;
        }
        ret=string_add_atring(container);
        return ret;

    }
protected:
    string string_multiply_char(string str,char ch,int i)
    {
       cout<<str<<" * "<<ch<<" i "<<i<<endl;
        string ret;
        string tail;
        for(int j=0;j<i;j++)
            tail.push_back('0');
        int a=0;//jin wei
        int y=ch-'0';
        int count = str.size()-1;
        stack<char> _stack;
        while(count>=0)
        {
            int x=str.at(count)-'0';
            cout<<"char x: "<<x<<endl;
            int mid = x*y;
            char z = (mid+a)%10+'0';
            a=(mid+a)/10;
            _stack.push(z);
            count--;
        }
        if(a>0)
            _stack.push(a+'0');
        while(!_stack.empty())
        {
            ret.push_back(_stack.top());
            _stack.pop();
        }
        ret+=tail;
        cout<<" ret "<<ret<<endl;
        return ret;
    }

    string string_add_atring(vector<string> res)
    {
        string str1;
        string str2;
        if(res.size()<2)
            return str1;
          str1=res[0];
        for(int i=1;i<res.size();i++)
        {
           str2=res[i];
            cout<<str1<<"+"<<str2<<endl;
           int m=str1.size();
           int n=str2.size();
           int count = min(m,n);
           int a=0;
           stack<char> _stack;
           for(int j=0;j<count;j++)
           {
               int x=str1.at(m-j-1)-'0';
               int y=str2.at(n-j-1)-'0';
               char mid=(x+y+a)%10+'0';
               cout<<"char mid"<<mid<<endl;
               a =(x+y+a)/10;
               cout<<"jin wei "<<a<<endl;
               _stack.push(mid);
           }
           if(m>n)
           {
               for(int k=m-n-1;k>=0;k--)
               {
                   char mid1=a+str1.at(k);
                   cout<<" mid1 "<<mid1<<endl;
                   _stack.push(mid1);
                   a=0;
               }
           }
           else if(m<n)
           {
               for(int l=n-m-1;l>=0;l--)
               {
                   char mid2=a+str2.at(l);
                   cout<<"mid2 "<<mid2<<endl;
                   _stack.push(mid2);
                   a=0;
               }
           }
           else
           {
               if(a>0)
               _stack.push(a+'0');
           }
           str1.clear();
           while(!_stack.empty())
           {
               str1.push_back(_stack.top());
               _stack.pop();
           }

        }//for
        return str1;
    }
};

方法二:比较简洁

class Solution {
public:
    string multiply(string num1,string num2) {
        reverse(num1.begin(),num1.end());
        reverse(num2.begin(),num2.end());
        
        if (("0" == num1) || ("0" == num2)) {
            return "0";
        }
        
        string result = "";
        int i,j;
        int flag = 0,steps = 0;
        
        for (int i = 0; i < num1.length(); ++i) {
            int position = steps;
            
            for (int j = 0; j < num2.length(); ++j) {
                int v = (num1[i] - '0') * (num2[j] - '0') + flag;

                if (position < result.length()) {
                    v += result[position] - '0';
                    result[position] = (v % 10) + '0';
                }
                else {
                    result.append(1,(v % 10) + '0');
                }
                
                flag = v / 10;
                ++position;
            }
            
            if (flag > 0) {
                result.append(1,flag + '0');
            }
            
            flag = 0;
            ++steps;
        }
        
        reverse(result.begin(),result.end());
        
        return result;
    }
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读