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

 

思路:首先,两个数都可以是无限大的,所以考虑用数字表示数字。开始看错题目了,以为是加法,所以现实了一个加法,就是add。

1 把字符串转成数组

2 数组逆序,这样方便从低位向高位计算

3 核心算法

4 结果数组逆序。

对于加法,核心算法相对简单,就不解释了。

对于乘法,解释下,假设是52*13,数组逆序后A[0] = 2, A[1]= 5, B[0]=3, B[1]= 1

A[i]*A[j]在结果中可能产生进位,所以保存在C[i+j+1]和C[i+j]中,然后所有Sum(A[i]*A[j])就是结果。核心代码就是

         for(int i = 0; i < len1; i++)
            {
                for(int j = 0; j < len2; j++)
                {
                    ret[i+j] += n1[i] * n2[j];
                    ret[i+j+1] += ret[i+j]/10;
                    ret[i+j] = ret[i+j]%10;
                }
            }

 

     52

*   13

---------------------

    156

  52

---------------------

 5356

class Solution {
    public:
        vector<int> string2IntVector(const string & num)
        {
            vector<int> ret;
            size_t len = num.size();
            ret.resize(len);
            for(int i = 0; i < len; i++)
            {
                ret[i] = num[i] - '0';
            }

            return ret;
        }

        string intVector2String(const vector<int> num)
        {
            string ret;
            size_t len = num.size();
            for(int i = 0; i < len; i++)
            {
                ret += num[i] + '0';
            }

            return ret;
        }

        string add(string num1, string num2)
        {
            int len1 = num1.size();
            int len2 = num2.size();

            vector<int> n1 = string2IntVector(num1);
            vector<int> n2 = string2IntVector(num2);

            reverse(n1.begin(), n1.end());
            reverse(n2.begin(), n2.end());


            vector<int> ret;

            int idx = 0;
            int carry = 0;
            while(idx < min(len1, len2))
            {
                //first calculate, then update carry flag
                ret.push_back((n1[idx] + n2[idx] + carry)%10);
                carry = (n1[idx] + n2[idx] + carry)/10;
                idx++;
            }

            while(idx < len1)
            {
                ret.push_back((n1[idx] + carry)%10);
                carry = (n1[idx] + carry)/10;
                idx++;
            }

            while(idx < len2)
            {
                ret.push_back((n2[idx] + carry)%10);
                carry = (n2[idx] + carry)/10;
                idx++;
            }

            reverse(ret.begin(), ret.end());
            return intVector2String(ret);
        }

        string multiply(string num1, string num2)
        {
            if(num1 == "0" || num2 == "0")
                return "0";
            int len1 = num1.size();
            int len2 = num2.size();

            vector<int> n1 = string2IntVector(num1);
            vector<int> n2 = string2IntVector(num2);

            reverse(n1.begin(), n1.end());
            reverse(n2.begin(), n2.end());

            vector<int> ret;

            ret.resize(len1 + len2);

            for(int i = 0; i < len1; i++)
            {
                for(int j = 0; j < len2; j++)
                {
                    ret[i+j] += n1[i] * n2[j];
                    ret[i+j+1] += ret[i+j]/10;
                    ret[i+j] = ret[i+j]%10;
                }
            }

            //delete useless 0
            int ZeroNum = 0;
            for(int i = len1 + len2 -1; i >= 0 ;i--)
            {
                if(ret[i] == 0)
                    ZeroNum ++;
                else
                    break;
            }
            ret.resize(len1 + len2 - ZeroNum);

            reverse(ret.begin(), ret.end());
            return intVector2String(ret);
        }
};

 思路2:可以用num1的某一位*num2,然后对所有的num1的所有位* num2*10的指数的结果进行求和,code可参考https://github.com/haoel/leetcode/blob/master/src/multiplyStrings/multiplyStrings.cpp

 

posted @ 2015-02-11 16:47  穆穆兔兔  阅读(173)  评论(0编辑  收藏  举报