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) {
        int len1 = remove_leading_zero(num1);
        int len2 = remove_leading_zero(num2);

        string muls[10];
        muls[0] = "0";
        for (int i=1; i<10; i++) {
            muls[i] = add(muls[i-1], num1);
        }
        
        string ending_zero(""), res("");
        int pos2 = len2 - 1;
        while (pos2 >= 0) {
            int c = num2[pos2--] - '0';
            res = add(res, muls[c] + ending_zero);
            ending_zero.insert(ending_zero.end(), '0');
        }
        return res;
    }
    
    string add(string num1, string num2) {
        string res("");
        int pos1 = remove_leading_zero(num1) - 1;
        int pos2 = remove_leading_zero(num2) - 1;

        int carry = 0;
        
        while (pos1 >=0 && pos2 >= 0) {
            int c1 = num1[pos1--] - '0';
            int c2 = num2[pos2--] - '0';
            int sum= c1 + c2 + carry;
            carry = sum / 10;
            sum = sum % 10;
            res.insert(res.begin(), sum + '0');
        }
        while (pos1 >=0 ) {
            int c = num1[pos1--] - '0';
            int s = c + carry;
            carry = s / 10;
            s = s % 10;
            res.insert(res.begin(), s + '0');
        }
        while (pos2 >=0 ) {
            int c = num2[pos2--] - '0';
            int s = c + carry;
            carry = s / 10;
            s = s % 10;
            res.insert(res.begin(), s + '0');
        }
        if (carry > 0 ) res.insert(res.begin(), carry + '0');
        return res;
    }
    
    int remove_leading_zero(string& str) {
        int len = str.length();
        int pos = 0;
        
        while(pos < (len - 1) && str[pos] == '0') pos++;
        len = len - pos;
        str = str.substr(pos, len);
        return len;
    }
};

200ms,用纯C的话应该可以再快一点,

方法不对,可以直接在各个数字位相乘时使用乘法:

class Solution {
public:
    string multiply(string num1, string num2) {
        int alen = num1.size();
        int blen = num2.size();
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        
        vector<int> res(alen + blen, 0);
        
        for (int i=0; i<alen; i++) {
            for (int j=0; j<blen; j++) {
                res[i + j] += (num1[i] - '0') * (num2[j] - '0');
            }
        }
        int pos = alen + blen - 1;
        while (pos >= 0 && res[pos] == 0) {
            pos--;
        }
        string str;
        int carry = 0;
        for (int i=0; i<=pos; i++) {
            res[i]= res[i] + carry;
            carry = res[i] / 10;
            str.push_back(res[i] % 10 + '0');
        }
        if (carry != 0) {
            str.push_back(carry + '0');
        }
        reverse(str.begin(), str.end());
        if (str.size() == 0) {
            str = "0";
        }
        return str;
    }
};

 

posted @ 2014-04-25 15:44  卖程序的小歪  阅读(164)  评论(0编辑  收藏  举报