leetcode#43. Multiplying strings

Topic link

Title description:

Given two non-negative integers num1 and num2 expressed in string form, return the product of num1 and num2, and their product is also expressed in string form.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Description:

The length of num1 and num2 is less than 110.
num1 and num2 only contain the numbers 0-9.
Neither num1 nor num2 start with a zero, unless it is the number 0 itself.
You cannot use any large number types of the standard library (such as BigInteger) or directly convert the input to an integer for processing.


Problem-solving ideas

  The difficulty of this question lies in how to restore our daily way of thinking, that is, the vertical multiplication that we learned from childhood. Only one violence law is provided here, the picture above~
Insert picture description here

  As can be seen from the figure, it is actually traversing the num2numbers, num1multiplying them and finally accumulating the results.
  In the item-by-item multiplying part, there is a problem to be considered- adding 0 . Including item-by-item multiplication and item-by-item accumulation are the continuation of #415. String addition , the solution is here~
  Simply put, three issues must be considered for both multiplication and addition:

  1. Carry problems, carry = (n1 * n2 + carry) / 10and carry = (n1 + n2 + carry) / 10both indicate the carry result after the current bit is multiplied or added;
  2. There are two parts to the most significant overflow problem. What should I do if a carry occurs after the calculation of the highest order is completed? Can be added in the for loop carry != 0, then this will lead to the second question, what if ior jhas been traversed? At this time, a ternary operator can be used to solve the problem n1 = j < 0 ? 0 : num1[j] - '0';
  3. The current position calculation problem, of course, product = (n1 * n2 + carry) % 10and the result is added to the tempend

  Also note that after the end of the loop, the result is a flashback, you need to reversecalculate the following first ~


Code

class Solution {
    
    
public:
    string multiply(string num1, string num2) {
    
    
        if(num1 == "0" || num2 == "0")
            return "0";

        string result = "";
        for(int i = num2.size() - 1; i >= 0; i--){
    
    
            int carry = 0;
            string temp = "";
            //补0
            for(int j = 0; j < num2.size() - 1 - i; j++){
    
    
                temp += '0';
            }
            int n2 = num2[i] - '0';
            for(int j = num1.size() - 1; j >= 0 || carry != 0; j--){
    
    
                int n1 = j < 0 ? 0 : num1[j] - '0';
                int product = (n1 * n2 + carry) % 10;
                temp += to_string(product);
                carry = (n1 * n2 + carry) / 10;
            }
            reverse(temp.begin(), temp.end());
            result = addString(result, temp);
        }

        return result;
    }

    string addString(string num1, string num2){
    
    
        string result = "";
        int carry = 0;
        for(int i = num1.size() - 1, j = num2.size() - 1; i >= 0 || j >= 0 || carry != 0; i--, j--){
    
    
            int n1 = i < 0 ? 0 : num1[i] - '0';
            int n2 = j < 0 ? 0 : num2[j] - '0';
            int temp = (n1 + n2 + carry) % 10;
            result += to_string(temp);
            carry = (n1 + n2 + carry) / 10;
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/104965965