当前位置:网站首页>C language force deduction question 43 string multiplication. Optimized vertical
C language force deduction question 43 string multiplication. Optimized vertical
2022-07-27 03:44:00 【Take care of two dogs and never let them go bad】
Given two non negative integers in the form of strings num1 and num2, return num1 and num2 The product of the , Their product is also represented as a string .
Be careful : You cannot use any built-in BigInteger Library or directly convert the input to an integer .
Example 1:
Input : num1 = "2", num2 = "3"
Output : "6"
Example 2:
Input : num1 = "123", num2 = "456"
Output : "56088"
Tips :
1 <= num1.length, num2.length <= 200
num1 and num2 It can only consist of numbers .
num1 and num2 It doesn't contain any leading zeros , In addition to digital 0 In itself .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/multiply-strings
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Use arrays instead of strings to store results , You can reduce the operation of strings .
Make m and n respectively num1 and num2 The length of , And none of them is 0, be num1 and num2 The length of the product of is m+n−1 or m+n. The simple proof is as follows :
—— If num1 and num2 Take the minimum , be num1=10^{m-1},num2=10^{n-1},num1×num2=10^{m+n-2}, The length of the product is m+n−1;
—— If num1 and num2 Take the maximum , be num1=10^m-1,num2=10^n-1,num1×num2=10^{m+n}-10^m-10^n+1, The product is obviously smaller than 10^{m+n} And greater than 10^{m+n-1}, So the length of the product is m+n.
because num1 and num2 The maximum length of the product of is m+n, Therefore, the creation length is m+n Array of ansArr Used to store the product . For any 0≤i<m and 0≤j<n,num1[i]×num2[j] The results are located in ansArr[i+j+1], If ansArr[i+j+1]≥10, Then add the carry part to ansArr[i+j].
Last , Will array ansArr Convert to string , If the highest level is 0 Then discard the highest position .
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
char * multiply(char * num1, char * num2)
{
int m = strlen(num1), n = strlen(num2);
char* ans = malloc(sizeof(char) * (m + n + 3));
memset(ans, 0, sizeof(char) * (m + n + 3));
int* ansArr = malloc(sizeof(int) * (m + n + 3));
memset(ansArr, 0, sizeof(int) * (m + n + 3));
int i = 0;
int ansLen = 0;
int index = 0;
if ((m == 1 && num1[0] == '0') || (n == 1 && num2[0] == '0'))
{
ans[0] = '0', ans[1] = 0;
return ans;
}
for ( i = m - 1; i >= 0; i--) {
int x = num1[i] - '0';
for (int j = n - 1; j >= 0; j--) {
int y = num2[j] - '0';
ansArr[i + j + 1] += x * y;
}
}
for ( i = m + n - 1; i > 0; i--) {
ansArr[i - 1] += ansArr[i] / 10;
ansArr[i] %= 10;
}
index = ansArr[0] == 0 ? 1 : 0;
ansLen = 0;
while (index < m + n) {
ans[ansLen++] = ansArr[index];
index++;
}
for ( i = 0; i < ansLen; i++) ans[i] += '0';
return ans;
}
int main()
{
char num1[] = "123";
char num2[] = "456";
char num3[1000] = "";
strcpy(num3,multiply(num1, num2));
printf("%s", num3);
return 0;
}边栏推荐
- MySQL underlying data structure
- Banyan loan,
- GetObject call timing of factorybean
- [机缘参悟-52]:交浅言深要因人而异
- [learn FPGA programming from scratch -54]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Altera)
- Tool class of localdatetime sorted out by yourself
- Textbox in easyUI inserts content at the cursor position
- Banyan data model of Bairong
- Reading notes of Kazuo Inamori's advice to young people
- Unity game, the simplest solution of privacy agreement! Just 3 lines of code! (Reprinted)
猜你喜欢

If the detailed explanation is generated according to the frame code

MySQL Chinese failure

快速排序及优化

The diagram of user login verification process is well written!

477-82(236、61、47、74、240、93)
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Contour detection based on OpenCV (1)

Briefly sort out the dualpivotquicksort

app端接口用例设计方法和测试方法

Contour detection based on OpenCV (2)
随机推荐
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
How to conduct 360 assessment
J-3-point practice in the second game of 2022 Niuke multi school
Daffodils (day 78)
mysql出现不存在错误
Unity game, the simplest solution of privacy agreement! Just 3 lines of code! (Reprinted)
MySQL的数据库有关操作
阶乘末尾0的数量
PIP3 setting alicloud
Two help points distribution brings to merchants
Explain详解
Quick sequencing and optimization
复盘:DFS与BFS的主要区别,在思想上的区别,代码实现上的区别
[understanding of opportunity -52]: the depth of communication varies from person to person
Characteristics and determination scheme of Worthington pectinase
Digital analog 1232
代码回滚,你真的理解吗?
Deep learning vocabulary embedded, beam search
Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
Food chain (day 79)