字符串相乘 - 中等

*************

C++

topic: 43. 字符串相乘 - 力扣 (LeetCode)

*************

Give the topic an inspection.

really bad days. I have done shadow reading in the past mounth, about an hour per day, and I can repeat one sentence per day normally. But things has changed in the past two days. I learned the sentence in the picture below for two days and I cannot repeat the sentence after trainning over and over. I almost quit. I ask myself why are you so silly. Just repeat the sentence has any challenge? And I learnt a word, calibrate, which means smooth the difference between the measure value and the standard value. That is good because I remember the word I learnt today.

Actually it was anothe week passed because of the bussiness trip. Really a busy trip. For the first time I saw the bananas in the real banana tree. So many different trees and flowers lay beside the roads.

OK, back to the inspecting, what is biginteger, in easy word, you cannot use function multiplication result = a * b. you neeed to simulate the process of the calculation. Manual calculation works.

   123
×   45
------
   615   (123 × 5)
  492    (123 × 4,注意这里需要左移一位)
------
  5535

And I remember that there is concept in the last learning, which is carry.

二进制求和 - 简单-优快云博客https://blog.youkuaiyun.com/ElseWhereR/article/details/147244403

And in mathematic rules, calculating should start from right. So just reverse the string, and start from i = 0, which makes sense.

class Solution {
public:
    string multiply(string num1, string num2) {
        
        // 反转string,模拟竖式计算从个位开始计算
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        // 经典获取string长度
        int m = num1.size();
        int n = num2.size();
        // 初始化一个结果,为0
        vector<int> result(m + n, 0); 

        // do something here
    }
};

To make it clear, I will make an example.

make num1 = 3, num2 = 138. and result vecor<int> = (0,0,0,0).

i = 0; j = 0; 3 * 8 = 24, vector[0] = 4, carry = 2, --> result = (4, 0【这里进2,下次计算这里得+2】, 0, 0);

i = 0; j = 1; 3 * 3 = 9, vector[1] = 9 + 2 = 11; vector[1] = 1, carry = 1, --> result = (4, 1, 0【下次计算,这里得+1】, 0);

i = 0; j = 2; 3 * 1 = 3; vector[2[ = 1 + 3 = 4;  --> result = (4, 1, 4, 0)

and result = 414, which is  3 * 138 = 414;

And it can be similar.

class Solution {
public:
    string multiply(string num1, string num2) {
        
        // 反转string,模拟竖式计算从个位开始计算
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        // 经典获取string长度
        int m = num1.size();
        int n = num2.size();
        // 初始化一个结果,为0
        vector<int> result(m + n, 0); 

        // do something here
        // 逐位相乘
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int digit1    = num1[i] - '0'; //  将字符转换为数字
                int digit2    = num2[j] - '0'; //  将字符转换为数字
                int product   = digit1 * digit2
                result[i + j] = result[i + j] + product;
            }
        }
    }
};

and go on to process the carry problem.

class Solution {
public:
    string multiply(string num1, string num2) {
        
        // 反转string,模拟竖式计算从个位开始计算
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        // 经典获取string长度
        int m = num1.size();
        int n = num2.size();
        // 初始化一个结果,为0
        vector<int> result(m + n, 0); 

        // do something here
        // 逐位相乘
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int digit1    = num1[i] - '0'; //  将字符转换为数字
                int digit2    = num2[j] - '0'; //  将字符转换为数字
                int product   = digit1 * digit2
                result[i + j] = result[i + j] + product;

                // 处理进位
                if (result > 9)
                {
                    int carry         = result[i + j] / 10; // 进位,比如13要进1
                    result[i + j]     = result[i + j] + result[i + j] % 10; // 保留个位数
                    result[i + j + 1] = result[i + j + 1] + carry;
                }
            }
        }
    }
};

and turn to string

class Solution {
public:
    string multiply(string num1, string num2) {
        
        // 反转string,模拟竖式计算从个位开始计算
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        // 经典获取string长度
        int m = num1.size();
        int n = num2.size();
        // 初始化一个结果,为0
        vector<int> result(m + n, 0); 

        // do something here
        // 逐位相乘
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int digit1    = num1[i] - '0'; //  将字符转换为数字
                int digit2    = num2[j] - '0'; //  将字符转换为数字
                int product   = digit1 * digit2;
                result[i + j] = result[i + j] + product;

                // 处理进位
                if (result[i + j] > 9)
                {
                    int carry         = result[i + j] / 10; // 进位,比如13要进1
                    result[i + j]     = result[i + j] + result[i + j] % 10; // 保留个位数
                    result[i + j + 1] = result[i + j + 1] + carry;
                }
            }
        }

        // 转换成字符串
        string reverseResult;
        int x = m + n - 1;
        while (x >= 0 && result[x] == 0)
        {
            x--;
        }

        for (x > 0; x--)
        {
            reverseResult = reverseResult + result[x] + '0';
        }

        return result;
    }
};

but something goes wrong.

And feed the ChatGpt this problem and she  will give you a small kiss.

She told me that carry method comes after multiply immediately makes error.

class Solution {
public:
    string multiply(string num1, string num2) {
        
        // 反转string,模拟竖式计算从个位开始计算
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        // 经典获取string长度
        int m = num1.size();
        int n = num2.size();
        // 初始化一个结果,为0
        vector<int> result(m + n, 0); 

        // do something here
        // 逐位相乘
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int digit1    = num1[i] - '0'; //  将字符转换为数字
                int digit2    = num2[j] - '0'; //  将字符转换为数字
                int product   = digit1 * digit2;
                result[i + j] = result[i + j] + product;

                // 处理进位
                if (result[i + j] > 9)
                {
                    int carry         = result[i + j] / 10; // 进位,比如13要进1
                    result[i + j]     = result[i + j] % 10; // 保留个位数
                    result[i + j + 1] = result[i + j + 1] + carry;
                }
            }
        }

        // 转换成字符串
        string reverseResult;
        int pos = result.size() - 1;
        while (pos >= 0 && result[pos] == 0)
        {
            pos--;
        }
        if (pos < 0) 
        {
            return "0";
        }
        for (int i = pos; i >= 0; --i) 
        {
            reverseResult.push_back(result[i] + '0');
        }

        return reverseResult;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值