String leetcode 43 string multiplication

topic

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

Example 1:

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

Example 2:

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

Explanation:
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.

Source: LeetCode
Link: https://leetcode-cn.com/problems/multiply-strings

The code written by Leetcode:

Big guy answer link

I copied the big guy code separately and added comments, as follows:

char * multiply(char * num1, char * num2)
{
    
    
    int len1 = strlen(num1);
    int len2 = strlen(num2);
    int totalLength = len1 + len2;

    int charIndex = 0;
    int valueIndex = 0;

    int *value = (int *)malloc(sizeof(int) * totalLength);
    //这里result必须动态申请内存,如果定义数组,返回的时候会被操作系统释放,或者用来干其他,导致打印null
    char *result = (char *)malloc(sizeof(char) * (totalLength + 1));
    //用于让value存放的全是0
    memset(value, 0, sizeof(int) * totalLength);

    for (int i = len1 - 1; i >= 0; i--){
    
    
        for (int j = len2 - 1; j >= 0; j--) {
    
    
        	//小学算两位数乘法的时候,用的方法,找出每次相乘的规律,相乘运算结果放在第I+j-1,这是在不超过10的情况下,超过了会给上一位均
            value[i + j + 1] += (num1[i] - '0') * (num2[j] - '0'); 
        }
    }

    for (int i = totalLength - 1; i >0; i--) {
    
    
    	//本位的值就是value[i] %= 10,进位是向i-1进位的
        value[i - 1] += value[i] / 10;
        value[i] %= 10;
    }
    while(valueIndex < totalLength - 1 && value[valueIndex] == 0) {
    
    
    	//算出刚开始几位是0的情况,把其去掉
        valueIndex++;
    }

    while(valueIndex < totalLength) {
    
    
    	//整数+'0'可以变成字符串
        result[charIndex++] = value[valueIndex++] + '0';
    }
    result[charIndex] = '\0';
    return result;
}

作者:moya
链接:https://leetcode-cn.com/problems/multiply-strings/solution/cyu-yan-by-moya-8/
来源:力扣(LeetCode)

to sum up:

The error I wrote myself broke my mentality, but I know that when a string needs to be returned in the function, use dynamic memory to apply, so that it will not be automatically released by the operating system, and what I did is first Convert num1 num2 into integers, and then multiply, but did not consider that the result of this multiplication will be very large, resulting in exceeding the range. Because the characters here can have 109 bits, the vertical multiplication is still effective.

The code I wrote failed because it did not take into account the very long string

#include<stdio.h>
#include<string.h>
char * multiply(char * num1, char * num2);
int main(void){
    
    
	char *num1="3";
	char *num2="4";
	char  *a;
	a=multiply(num1,num2);
	printf("%s",a);
} 

char * multiply(char * num1, char * num2){
    
    
    int num1len = strlen(num1);
    int num2len = strlen(num2);
    int total=0;
    char *str = (char *)malloc(sizeof(char)*(num1len+num2len+1));
    //static char str[201];  这样可以.但是这里[] 内容必须填写常量. 
    //char str[num1len+num2len+1]  错误的做法 
    int count=0;
    int n1 = 0;
    int n2 = 0;
    int b;
    int i,j,c;
    if(num1[0]=='0' || num2[0]=='0')
        return "0";
     // num1转换成整数
    for(i=0;i<num1len;i++){
    
    
        c=num1[i]-'0';
        n1=10*n1+c;
    }
    // num2转换成整数
    for(i=0;i<num2len;i++){
    
    
        c=num2[i]-'0';
        n2=10*n2+c;
    }
    //相乘的结果
    total=n1*n2;
    // 开始转换成字符串
    while(total!=0){
    
    
        str[count]='0'+(total%10);
        total=total/10;
        count++;
    }
    str[count]='\0';
    if(count==1)
        return str;
     // 交换位置,因为2*7 程序会输出41
    for(j = 0; j <(count/2); j++ )
    {
    
    
        b = str[j];
       str[j] = str[count - 1 - j];
         str[count - 1 - j] = b;
    }
    return str;
    }

Guess you like

Origin blog.csdn.net/mogbox/article/details/112982345