LeetCode //C - 43. Multiply Strings

该篇文章介绍了如何在LeetCode43题中,不使用内置BigInteger库的情况下,通过迭代和数组操作,将两个表示非负整数的字符串相乘并返回结果字符串。解决方案包括处理零、乘法运算、进位处理以及最终字符串格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

43. Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
 

Example 1:

Input: num1 = “2”, num2 = “3”
Output: “6”

Example 2:

Input: num1 = “123”, num2 = “456”
Output: “56088”

Constraints:
  • 1 <= num1.length, num2.length <= 200
  • num1 and num2 consist of digits only.
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.

From: LeetCode
Link: 43. Multiply Strings


Solution:

Ideas:
  1. It first checks if either input string is “0”. If so, the result is “0”.
  2. It calculates the lengths of the input strings and allocates an array to store the intermediate product results.
  3. It iterates through each digit of the two numbers, multiplies the digits, and adds them to the appropriate position in the result array, taking care of positional value by using array indices.
  4. It then converts the array into a string, adjusting for carries in the digits.
  5. Finally, it formats the result into a string, handling leading zeros if necessary.
Code:

char* multiply(char* num1, char* num2) {
    // If one of the numbers is "0", the result is "0"
    if (strcmp(num1, "0") == 0 || strcmp(num2, "0") == 0) {
        char* result = malloc(2);
        strcpy(result, "0");
        return result;
    }

    int len1 = strlen(num1);
    int len2 = strlen(num2);
    int lenResult = len1 + len2;
    
    // Array to store result of multiplication
    int *result = calloc(lenResult, sizeof(int));

    // Multiply each digit from num1 by each digit from num2 and store in result
    for (int i = len1 - 1; i >= 0; i--) {
        for (int j = len2 - 1; j >= 0; j--) {
            int index = (len1 - 1 - i) + (len2 - 1 - j);
            int mul = (num1[i] - '0') * (num2[j] - '0');
            result[index] += mul;
        }
    }

    // Handle carry and convert int array to string
    for (int i = 0; i < lenResult - 1; i++) {
        result[i + 1] += result[i] / 10;
        result[i] %= 10;
    }

    // Remove leading zeros and prepare final string
    int start = lenResult - 1;
    while (start >= 0 && result[start] == 0) {
        start--;
    }
    char* finalResult = malloc(start + 2); // +1 for null terminator
    for (int i = start; i >= 0; i--) {
        finalResult[start - i] = result[i] + '0';
    }
    finalResult[start + 1] = '\0';

    // Free the temporary result array
    free(result);
    
    return finalResult;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值