力扣第四十三题(字符串相乘)详解

 根据题意,我们可以将其拆解为num1乘以num2中的每一个数,再将其相加可得,这个思路是我们做乘法时最常见的思路,其中需要的注意的是当从右遍历到左时,除最右边的数,其他的数相乘后需要在其后面加上0,用于补位。当然还有另一种直接用数组来储存乘后的结果,来优化代码。

方法一:

先将其每一位转化为整型,再逐个相乘,同时判断是否进位,最后算得每一位num2乘以num1的值,最后将其相加得到最终的结果。

class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0") {
            return "0";
        }
        string ans = "0";
        int m = num1.size(), n = num2.size();
        for (int i = n - 1; i >= 0; i--) { //从最右边向前遍历
            string curr;
            int add = 0; 
            for (int j = n - 1; j > i; j--) {
                curr.push_back(0);  //除最右边的数外其他的数都要加‘0’,用于补位
            }
            int y = num2.at(i) - '0'; //将字符转化为int整数
            for (int j = m - 1; j >= 0; j--) {
                int x = num1.at(j) - '0';
                int product = x * y + add; //将num1的字符和num2的字符相乘,同时加上进位
                curr.push_back(product % 10);//将相乘得到的数取模
                add = product / 10;//得到进位的数
            }
            while (add != 0) { //当num1乘完后,还有进位
                curr.push_back(add % 10);
                add /= 10;
            }
            reverse(curr.begin(), curr.end());
            for (auto &c : curr) {
                c += '0';//转换为字符
            }
            ans = addStrings(ans, curr);//将相乘所得到的数相加
        }
        return ans;
    }

    string addStrings(string &num1, string &num2) {
        int i = num1.size() - 1, j = num2.size() - 1, add = 0;
        string ans;
        while (i >= 0 || j >= 0 || add != 0) {
            int x = i >= 0 ? num1.at(i) - '0' : 0;
            int y = j >= 0 ? num2.at(j) - '0' : 0;
            int result = x + y + add;
            ans.push_back(result % 10);
            add = result / 10;
            i--;
            j--;
        }
        reverse(ans.begin(), ans.end());
        for (auto &c: ans) {
            c += '0';
        }
        return ans;
    }
};

方法二:

由于m位整数和n位整数相乘后得到的结果为m+n位或m+n-1位,所以我们可以用m+n大小的数组储存相乘后的结果,然后再从后向前遍历,将其取模进位,最后转化为字符。

class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1 == "0" || num2 == "0"){
            return "0";
        }
        int m = num1.size(),n = num2.size();
        auto ansArr = vector<int>(m + n);
        for(int i = m - 1;i >= 0;i--){
            int x = num1.at(i) - '0';
            for(int j = n - 1;j >= 0;j--){
                int y = num2.at(j) - '0';
                ansArr[i + j + 1] += x * y;//将其以int整数形式储存到数组中
            }
        }
        for(int i = m + n - 1;i > 0;i--){
            ansArr[i - 1] += ansArr[i] / 10; //将其取模进位
            ansArr[i] %= 10;
        }
        int index = ansArr[0] == 0 ? 1 : 0;//判断为m+n型还是m+n-1型
        string ans;
        while(index < m + n){
            ans.push_back(ansArr[index]);
            index++;
        }
        for(auto &c : ans){
            c += '0';
        }
        return ans;
    }
};

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值