(69)43. 字符串相乘(leetcode)

题目链接:
https://leetcode-cn.com/problems/multiply-strings/
难度:中等
43. 字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
示例 1:
	输入: num1 = "2", num2 = "3"
	输出: "6"
示例 2:
	输入: num1 = "123", num2 = "456"
	输出: "56088"
说明:
	num1 和 num2 的长度小于110。
	num1 和 num2 只包含数字 0-9。
	num1 和 num2 均不以零开头,除非是数字 0 本身。
	不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。

不算难 做出来了 但是! 为什么我写的运行时间贼高?! 日 终究还是菜!

class Solution {
    
    
public:
	// 这个 addString 之前做过
    string addStrings(string num1, string num2) {
    
    
        int len1 = num1.size();
        int len2 = num2.size();
        int i = len1 - 1, j = len2 - 1;
        int c = 0;
        string res;
        while (i >= 0 && j >= 0)
        {
    
    
            int a = num1[i--] - '0' + num2[j--] - '0' + c;
            res += a % 10 + '0';
            c = a / 10;            
        }
        while (i >= 0) {
    
    
            int a = num1[i--] - '0' + c;
            res += a % 10 + '0';
            c = a / 10;
        }
        while (j >= 0) {
    
    
            int a = num2[j--] - '0' + c;
            res += a % 10 + '0';
            c = a / 10;
        }        
        if (c > 0) {
    
    
            res += c + '0';
        }
        reverse(res.begin(), res.end());
        return res;
    }
    string multiply(string num1, string num2) {
    
    
        if(num1=="0"||num2=="0"){
    
    
            return "0";
        }
        string ans="0";
        int n1=num1.size();
        int n2=num2.size();
        for(int i=n2-1;i>=0;--i){
    
    
            string cur="";
            // 进位标志
            int add=0;
            int y=num2.at(i)-'0';
            // num1
            for(int j=n1-1;j>=0;--j){
    
    
                int x=num1.at(j)-'0';
                int num=x*y+add;
                cur+=(num%10+'0');
                add=num/10;
            }
            if(add){
    
    
                cur+=(add+'0');
            }
            reverse(cur.begin(),cur.end());
            for(int k=n2-1;k>i;--k){
    
    
                cur+="0";
            }
            ans=addStrings(ans,cur);
        }
        return ans;
    }
};

发现一个更优的解法(感觉靠我自己想不出来这种解法。。。 至少现在不行 )
乘法 可以手动模拟一下

class Solution {
    
    
public:
    string multiply(string num1, string num2) {
    
    
        if(num1=="0"||num2=="0"){
    
    
            return "0";
        }
        int n1=num1.size();
        int n2=num2.size();
        // n1位*n2位  最大是n1+n2位的 最小是n1+n2-1位的
        // 记录每一位的值 最左侧位最高位
        vector<int> result(n1+n2,0);
        // 枚举 num1和num2
        for(int i=n1-1;i>=0;--i){
    
    
            int x=num1.at(i)-'0';
            for(int j=n2-1;j>=0;--j){
    
    
                int y=num2.at(j)-'0';
                // 为什么是i+j+1?
                // 可以试一下 num1[i] * num2[j]
                // (i ,j 对数值来说(从右向左)分别是第n1-i n2-j 位 )
                // 对于result来说应该在(下标):(n1+n2)-(n1-i+n2-j-1)=i+j+1
                // 注意此处没有考虑进位 如 6*9 等 
                // 所以result中可能存在result[x]>10 
                result[i+j+1]+=x*y;
            }
        }
        // 去除每一位中大于10的数值
        for(int i=n1+n2-1;i>0;--i){
    
    
            result.at(i-1)+=result.at(i)/10;
            result.at(i)%=10;
        }
        string ans="";
        int i=0;
        if(!result.at(i)){
    
    
            i=1;
        }
        while(i<n1+n2){
    
    
            ans+=(result.at(i)+'0');
            i++;
        }
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/li_qw_er/article/details/107971300
今日推荐