43. 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.
  • Convertingthe input string to integer is NOT allowed.
  • You should NOT use internal library such as BigInteger.
//下面方法太复杂,超时
class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1.empty() || num2.empty()) return "-1";
        if(num1 == "0" || num2 == "0") return "0";
        string result;
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        for(int i = 0; i < num1.size();i++){
            string str(i,0);
            int cur = num1[i] - '0';
            int carry =0;
            for(int j = 0; j < num2.size();j++){
                int cur2 = (cur*(num2[j]-'0') + carry)%10;
                carry = (cur*(num2[j]-'0') + carry) / 10;
                str.push_back(cur2 + '0');
            }
            if(carry > 0) str.push_back(carry + '0');
            result = add(result,str);
        }
        reverse(result.begin(), result.end());
        return result;
    }
    string add(string str1,string str2){
        int carry = 0;
        int i =0;
        string result;
        while(i < str1.size() && i < str2.size()){
            int cur = (str1[i] + str2[i] - '0' + carry) %10;
            carry = (str1[i] + str2[i] - '0' + carry) /10;
            result.push_back(cur + '0');
        }
        while(i < str1.size()){
            int cur = (str1[i] - '0' + carry) %10;
            carry = (str1[i] - '0' + carry) /10;
        }
        while(i < str2.size()){
            int cur = (str2[i] - '0' + carry) %10;
            carry = (str2[i] - '0' + carry) /10;
        }
        if(carry > 0) result.push_back(carry + '0');
        return result;
    }
};

方法二

//简化版直接计算
class Solution {
public:
    string multiply(string num1, string num2) {
        string result(num1.size() + num2.size() ,'0');
        for(int i= num1.size()-1; i >=0;i--){
            int carry = 0;
            for(int j = num2.size()-1;j >=0;j--){
                int temp = (result[i+j+1] - '0') + (num1[i]-'0')*(num2[j] - '0') + carry;
                result[i+j+1] = temp%10 + '0';
                carry = temp /10;
            }
            result[i] += carry;
        }
        int start_pos = result.find_first_not_of('0');
        if (string::npos != start_pos) {
            return result.substr(start_pos);
        }
        return "0";
    }
};

方法三
这里写图片描述

 `num1[i] * num2[j]` will be placed at indices `[i + j`, `i + j + 1]` 
class Solution {
public:
    string multiply(string num1, string num2) {
        int m = num1.size();
        int n = num2.size();
        vector<int> pos(m+n);
        for(int i = m-1; i >=0; i--){
            for(int j = n-1; j >=0;j--){
                int mul = (num1[i] -'0')*(num2[j]- '0');
                int sum = pos[i+j+1] + mul;
                pos[i+j] += sum/10;
                pos[i+j+1] = sum%10; 
            }
        }
        string result;
        for(auto p : pos){
            if(!(result.empty() && p==0)) result.push_back(p + '0');
        }
        return result.empty()?"0":result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值