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;
    }
};
相关推荐
203号居民1 小时前
LeetCode hot 100 —41. 缺失的第一个正数
数据结构·算法·leetcode
专注仿真3 小时前
问答大模型技术方案算法实现-熵权法融合算法 + 交叉编码器重排算法
人工智能·python·算法·语言模型·问答大模型关键算法·熵权融合算法·交叉编码器重排算法
地平线开发者4 小时前
量化为什么会有损失:从量化误差到精度下降
算法
手写码匠5 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 会话管理与上下文工程实战:让智能体“记住该记住的,忘掉该忘掉的“
人工智能·深度学习·算法·aigc
2401_862880825 小时前
数据结构 --- 栈
c语言·数据结构·算法
致Great5 小时前
Qwen3.8-27B 接入 DSH 全流程实测:写代码、跑长任务,都没问题
算法
Tisfy6 小时前
LeetCode 3471.找出最大的几近缺失整数:三种情况判断
算法·leetcode·题解·分类讨论
linux-hzh6 小时前
百日算法修炼 · Day 12
算法·深度优先
额额额对了6 小时前
数据结构:二叉树
c语言·数据结构·算法
iNeuOS工业互联网7 小时前
iNeuOS_Vision_视觉分析,增加SAM3模型对实例分割和目标检测AI自动标注图片样本功能
人工智能·算法·目标检测