If you want to laugh, see my solution


  • 0
    W
    class Solution {
    string small_multiply(char cur, string nums){
        string result;
        int cur_num = cur - '0';
        int carry = 0;
        for (int i = nums.size() - 1; i >= 0; i--){
            int num = nums[i] - '0';
            if (num * cur_num + carry >= 10){
                result.push_back((num * cur_num + carry) % 10 + '0');
                carry = (num * cur_num + carry) / 10;
            } else{
                result.push_back((num * cur_num + carry) + '0');
                carry = 0;
            }
        }
        if (carry > 0){
            result.push_back(carry + '0');
        } 
        reverse(result.begin(), result.end());
        return result;
    }
    string addhelper(string nums1, string nums2, int pos){
        string result;
        int ind1 = nums1.size() - 1;
        int ind2 = nums2.size() - 1;
        for (int i = 0; i < pos; ++i){
            result.push_back(nums1[ind1]);
            ind1--;
        }
        int carry = 0;
        while (ind1 >= 0 && ind2 >= 0){
            int num1 = nums1[ind1] - '0';
            int num2 = nums2[ind2] - '0';
            if (num1 + num2 + carry >= 10){
                result.push_back((num1 + num2 + carry) % 10 + '0');
                carry = (num1 + num2 + carry) / 10;
            } else{
                result.push_back((num1 + num2 + carry) + '0');
                carry = 0;
            }
            ind1--;
            ind2--;
        }
        while (ind1 >= 0){
            int num1 = nums1[ind1] - '0';
            if (num1 + carry >= 10){
                result.push_back((num1 + carry) % 10 + '0');
                carry = (num1 + carry) / 10;
            } else{
                result.push_back((num1 + carry) + '0');
                carry = 0;
            }
            ind1--;
        }
        while (ind2 >= 0){
            int num2 = nums2[ind2] - '0';
            if (num2 + carry >= 10){
                result.push_back((num2 + carry) % 10 + '0');
                carry = (num2 + carry) / 10;
            } else{
                result.push_back((num2 + carry) + '0');
                carry = 0;
            }
            ind2--;
        } 
        if (carry > 0){
             result.push_back(carry + '0');
        }
        reverse(result.begin(), result.end());
        return result;
    }
    

    public:
    string multiply(string num1, string num2) {
    if (num1 == "0" || num2 == "0"){
    return "0";
    }
    vector<string> temp;
    for (int i = num1.size() - 1; i >= 0; --i){
    temp.push_back(small_multiply(num1[i], num2));
    }
    string result;
    result = temp[0];
    for (int i = 1; i < temp.size(); ++i){
    result = addhelper(result, temp[i], i);
    }
    int flag = false;
    int ind = 0;
    for (int i = 0; i < result.size(); ++i){
    if(result[i] == '0' && flag == false){
    ind++;
    }
    if (result[i] != '0'){
    flag = true;
    }
    }
    string result2(result.begin() + ind, result.end());
    return result2;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.