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

思路:

模拟乘法的过程。首先第二个数的每一位都与第一个数求乘积;其次,每个结果移位相加。由于数字非负,计算难度大大降低。

代码:

class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1.size()==0&&num2.size()==0)
        {
            return "0";
        }
        else if(num1.size()==0)
        {
            return "0";
        }
        else if(num2.size()==0)
        {
            return "0";
        }
        else
        {
            string result;
            for(int i=0;i<num2.size();i++)
            {
                result = add(result,mutiplyOneDigit(num1,num2[i],num2.size()-1-i));
            }
            int i=0;
            for(;i<result.size();i++)
            {
                if(result[i]!='0')
                {
                    break;
                }
            }
            if(i==result.size())
            {
                return "0";
            }
            else
            {
                return result.substr(i,result.size()-i);
            }
        }
        
    }
    
    string mutiplyOneDigit(string num1, char c, int digits)
    {
        int carry=0;
        string result;
        int cur1 = num1.size()-1;
        while(cur1>=0)
        {
            int t = (num1[cur1]-'0') * (c -'0') + carry;
            result.push_back(t%10+'0');
            carry = t/10;
            cur1--;
        };
        if(carry>0)
        {
            result.push_back(carry+'0');
        }
        for(int i=0;i<result.size()/2;i++)
        {
            int temp = result[i];
            result[i] = result[result.size()-1-i];
            result[result.size()-1-i] = temp;
        }
        for(int i=0;i<digits;i++)
        {
            result.push_back('0');
        }
        return result;
    }
    
    string add(string num1, string num2) {
        if(num1.size()==0&&num2.size()==0)
        {
            return "";
        }
        else if(num1.size()==0)
        {
            return num2;
        }
        else if(num2.size()==0)
        {
            return num1;
        }
        else
        {
            int carry=0;
            string result;
            int cur1 = num1.size()-1;
            int cur2 = num2.size()-1;
            while(cur1>=0&&cur2>=0)
            {
                int t = num1[cur1]-'0' + num2[cur2] -'0' + carry;
                result.push_back(t%10+'0');
                carry = t/10;
                cur1--;
                cur2--;
            };
            while(cur1>=0)
            {
                int t = num1[cur1]-'0' + carry;
                result.push_back(t%10+'0');
                carry = t/10;
                cur1--;
            };
            while(cur2>=0)
            {
                int t = num2[cur2]-'0' + carry;
                result.push_back(t%10+'0');
                carry = t/10;
                cur2--;
            };
            if(carry>0)
            {
                result.push_back(carry+'0');
            }
            for(int i=0;i<result.size()/2;i++)
            {
                int temp = result[i];
                result[i] = result[result.size()-1-i];
                result[result.size()-1-i] = temp;
            }
            return result;
        }
    }
};
    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/17558855
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞