LeetCode 刷题【43. 字符串相乘】

43. 字符串相乘

自己做

解1:矩阵计数

java 复制代码
class Solution {
public:
    string multiply(string num1, string num2) {
        int len1 = num1.size();
        int len2 = num2.size();

        if (num1[0] == '0' || num2[0] == '0')       //结果为0的情况
            return "0";

        //存储计算过程的矩阵
        vector<vector<int>> calculation(len1, vector<int>(len1 + len2, 0));
        string string_res;    //存放结果

        //计算
        for (int i = len1 - 1; i >= 0; i--) {
            int add = 0;          //进位   
            for (int j = len2 - 1; j >= 0; j--) {
                int res = (num1[i] - 48) * (num2[j] - 48) + add;     //当前结果
                //cout << res << " ";
                calculation[len1 - 1 - i][i + j + 1] = res % 10;   //余位
                add = res / 10;                                     //进位
            }

            if(add > 0)                                       //进位有多
                calculation[len1 - 1 - i][i] = add;   
            //cout << endl;

        }

        //// 输出二维向量
        //cout << "calculation = [" << endl;
        //for (int i = 0; i < calculation.size(); ++i) {
        //    cout << "  [";
        //    for (int j = 0; j < calculation[i].size(); ++j) {
        //        cout << calculation[i][j];
        //        if (j != calculation[i].size() - 1) {
        //            cout << ", ";
        //        }
        //    }
        //    cout << "]" << (i == calculation.size() - 1 ? "" : ",") << endl;
        //}
        //cout << "]" << endl;


        //累加矩阵所有元素
        int add = 0;          //累加的进位
        for (int i = len1 + len2 - 1; i >= 0; i--) {
            int res = 0;          //这一轮累加的结果

            for (int j = 0; j < len1; j++) 
                res += calculation[j][i];

            res += add;   //加上进位

            string_res.insert(string_res.begin(), res % 10 + 48);     //余位存放进结果
            add = res / 10;                                           //进位更新
        }


        if (add > 0)
            string_res.insert(string_res.begin(), add + 48);     //余位存放进结果

        //消除前面的零
        while (string_res[0] == '0')
            string_res.erase(string_res.begin());

        return string_res;
    }
};

解2:优化解1

cpp 复制代码
class Solution {
public:
    string multiply(string num1, string num2) {
        int len1 = num1.size();
        int len2 = num2.size();

        if (num1[0] == '0' || num2[0] == '0')       //结果为0的情况
            return "0";

        string string_res(len1 + len2, '0');    //存放结果,结果最长也只是两者长度的和,不可能更长

        //计算
        for (int i = len1 - 1; i >= 0; i--) {
            int add = 0;          //进位   

            for (int j = len2 - 1; j >= 0; j--) {
                int int_res = (num1[i] - 48) * (num2[j] - 48) + (string_res[i + j + 1] - 48) + add;     //当前结果

                string_res[i + j + 1] = int_res % 10 + 48;                //余位存放进结果
                add = int_res / 10;                                     //进位
            }

            if (add > 0)                                      //进位有多
                string_res[i] = add + 48;
        }

        //消除前面的零
        while (string_res[0] == '0')
            string_res.erase(string_res.begin());

        return string_res;
    }
};
相关推荐
leke200322 分钟前
2025年10月17日
算法
CoovallyAIHub23 分钟前
Mamba-3震撼登场!Transformer最强挑战者再进化,已进入ICLR 2026盲审
深度学习·算法·计算机视觉
Aqua Cheng.28 分钟前
代码随想录第七天|哈希表part02--454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
java·数据结构·算法·散列表
怀揣小梦想28 分钟前
跟着Carl学算法--哈希表
数据结构·c++·笔记·算法·哈希算法·散列表
Nebula_g29 分钟前
Java哈希表入门详解(Hash)
java·开发语言·学习·算法·哈希算法·初学者
Kent_J_Truman29 分钟前
【模拟散列表】
数据结构·算法·蓝桥杯·散列表·常识类
Lchiyu34 分钟前
哈希表 | 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和
算法
玩镜的码农小师兄35 分钟前
[从零开始面试算法] (04/100) LeetCode 136. 只出现一次的数字:哈希表与位运算的巅峰对决
c++·算法·leetcode·面试·位运算·hot100
RTC老炮1 小时前
webrtc弱网-AcknowledgedBitrateEstimatorInterface类源码分析与算法原理
网络·算法·webrtc
Antonio9152 小时前
【图像处理】常见图像插值算法与应用
图像处理·算法·计算机视觉