[LeetCode-String] String multiplication

Title description

Given two non-negative integers num1 and num2 expressed as strings, return the product of num1 and num2, and their product is also expressed as a string.
Examples:

输入: num1 = "2", num2 = "3"
输出: "6"

输入: num1 = "123", num2 = "456"
输出: "56088"

Title link: https://leetcode-cn.com/problems/multiply-strings/
You can add strings before doing this question .

Idea 1

Simulators do multiplication, multiplying two numbers, first multiplying the first digit and the last digit of the second digit, then first multiplying the first digit and the penultimate digit of the second digit and left Shift one bit and add to the previous result, here we need to add the large integer.

code show as below:

class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1=="0" || num2=="0") return "0";
        if(num1=="1") return num2;
        if(num2=="1") return num1;
        
        string ans = "";
        for(int i=num2.size()-1, j=0; i>=0; i--){
            string mulRes = mul(num1, num2[i]);
            for(int k=0; k<j; k++){ // 左移相当于后面补零
                mulRes += "0";
            }
            ans = add(ans, mulRes);
            j++;
        }
        return ans;
    }

    /*字符串str和字符c相乘,返回相乘后的结果*/
    string mul(string str, char c){
        int carry = 0;
        string res = "";
        for(int i=str.length()-1; i>=0; i--){
            int t = (str[i]-'0')*(c-'0')+carry;
            res = to_string(t%10)+res;
            carry = t/10;
        }
        if(carry!=0) res = to_string(carry)+res;
        return res;
    }

    /*字符串s1和字符串s2相加,返回相加后的结果*/
    string add(string s1, string s2){
        if(s1=="" || s1=="0") return s2;
        if(s2=="" || s2=="0") return s1;
        
        int l1 = s1.length()-1;
        int l2 = s2.length()-1;
        int carry = 0;
        string ans = "";
        while(l1>=0 || l2>=0){
            int x = (l1>=0)? s1[l1]:'0';    // 注意是'0',不是0
            int y = (l2>=0)? s2[l2]:'0';    // 注意是'0',不是0
            int s = (x-'0')+(y-'0')+carry;
            ans = to_string(s%10)+ans;
            carry = s/10;
            if(l1>=0) l1--;
            if(l2>=0) l2--;
        }
        if(carry!=0) ans = "1"+ans;
        return ans;
    }
};
  • Time complexity: O (mn)
    m, n are the length of num1 and num2, respectively.
  • Space complexity: O (m + n)
    m, n are the length of num1 and num2, respectively.

Idea 2

Nature of use:

  • The maximum length of the result of multiplying the number of length n and the number of length m is m + n;
  • The result is stored in the array res. The result of num1 [i] * num2 [j] is a two-digit tmp (0x or xy), where the first bit is res [i + j] and the second bit is res [i + j + 1].

    code show as below:
class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1=="0" || num2=="0") return "0";
        if(num1=="1") return num2;
        if(num2=="1") return num1;
        
        /// int* res = new int[num1.length()+num2.length()]; // 用new会错,不清楚原因
        int res[num1.length()+num2.length()];
        memset(res, 0, sizeof(res));
        for(int i=num1.length()-1; i>=0; i--){
            int n1 = num1[i]-'0';
            for(int j=num2.length()-1; j>=0; j--){
                int n2 = num2[j]-'0';
                int s = res[i+j+1]+n1*n2;
                res[i+j+1] = s%10;  // 低位
                res[i+j] += s/10;   // 高位
            }
        }

        //也可以用stringstream来连接字符串
        /*stringstream ss;
        for(int i=0; i<num1.length()+num2.length(); i++){
            if(i==0 && res[i]==0) continue;
            ss<<res[i];
        }
        return ss.str();*/

        string ans = "";
        for(int i=0; i<num1.length()+num2.length(); i++){
            if(i==0 && res[i]==0) continue;
            ans += to_string(res[i]);
        }
        return ans;
    }
};
  • Time complexity: O (mn)
    m, n are the length of num1 and num2, respectively.
  • Space complexity: O (m + n)
    m, n are the length of num1 and num2, respectively.

Related topics

1. String addition: https://leetcode-cn.com/problems/add-strings/, the solution .

reference

https://leetcode-cn.com/problems/multiply-strings/solution/you-hua-ban-shu-shi-da-bai-994-by-breezean/

Guess you like

Origin www.cnblogs.com/flix/p/12687561.html