Multiply Strings

在这里插入图片描述
解析:本题大整数乘法,难点在于连续进位的处理
第一种解法:
例如123×456,根据乘法的运算,得知

  1. 3×6的起始数位是个位,而2×6的起始数位为十位,1×6的起始数位百位,观察发现,若用i表示123的索引,j表示456的索引,则当前两个数相乘的结果的当前数位为(i+j)
  2. 连续进位的处理,由于这里使用字符表示当前的结果,则出现结果大于或等于10,就要进位,由于采用单个字符只能表示0-9的数字,故还需进一步判断高位是否也存在进位,即9999+90,当前十位进位,导致后面百位和千位也会发生进位,所以也要进行处理。当然如果这里能用整形的变量表示就无须考虑
class Solution {
public:
    string multiply(string num1, string num2) {
        string res = "";
        if  (num1 == "0" || num2 == "0") return "0";
        
        if (num1.length() > num2.length()) swap(num1, num2);
        reverse(num1.begin(), num1.end()); //字符串翻转,保证从低位开始相乘
        reverse(num2.begin(), num2.end());
        for (int i = 0; i < num1.length(); ++i){
            for (int j = 0; j < num2.length(); ++j){
                int k = i + j;
                int current = (num1[i]-'0') * (num2[j]-'0'); //计算数位相乘
                if (k >= res.length()){ //当前是最高位
                    res += to_string(current % 10);
                    if (current >= 10) res += to_string(current / 10);
                    continue;
                }
                current += (res[k] - '0'); //字符—>数字
                res[k] = (current % 10) + '0'; //当前位
                bool flag = false;
                while (current >= 10){ //考虑多次进位,例如9999+90
                    res[k] = (current % 10) + '0';
                    if (++k >= res.length()){
                        flag = true;
                        res += to_string(current / 10);
                        break;
                    }
                    current = current / 10 + (res[k] - '0');  //进制位             
                }
                if (flag == false) res[k] = (current + '0');
            }
        }
        reverse(res.begin(), res.end()); //翻转得到正常的顺序
        return res;
    }
};

第二种解法
该解法无需考虑每次都要处理连续进位,采用整形变量代替字符存储结果,即采用vector数组存储结果,例如999×11,当出现999+90时,当前vector[1] = 8,而vector[2] = 10,然后在下一位9*10的时候再进行处理vector[2],就可以避免多次处理连续进位的情况。

class Solution {
public:
    string multiply(string num1, string num2) {
        int m = num1.length();
        int n = num2.length();
        string str_res = "";
        vector<int> res(m + n, 0);
        for (int i = m - 1; i >= 0; --i){
            for (int j = n - 1; j >= 0; --j){
                int mul = (num1[i]-'0') * (num2[j]-'0');
                int current_index = i + j;
                mul += res[current_index+1];
                res[current_index+1] = mul % 10;
                res[current_index] += mul / 10;
            }
        }
        for (int i = 0; i < res.size(); ++i){
            if (!str_res.empty() || res[i] != 0) str_res.push_back(res[i] + '0');
        }
        return str_res.empty() ? "0" : str_res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值