当前位置:网站首页>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;
}边栏推荐
- C语言力扣第43题之字符串相乘。优化竖式
- Indexing best practices
- Two help points distribution brings to merchants
- Volatile keyword and its function
- Introduction to database - Introduction to database
- Ring counting (Northern Polytechnic machine test questions) (day 83)
- 数字孪生应用及意义对电力的主要作用,概念价值。
- Banyan loan,
- DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
- Data analysis and disassembly method of banyan tree in Bairong
猜你喜欢

redis入门练习

Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image

Contour detection based on OpenCV (2)

Detailed explanation of const usage in C language

Explain

若依的环境的部署以及系统的运行

Introduction to database - Introduction to database

Fastboot刷机

【无标题】JDBC连接数据库读超时

flask_restful中reqparse解析器继承
随机推荐
Six determination methods of Worthington peroxidase activity
Typescript TS basic knowledge interface, generics
Member array and pointer in banyan loan C language structure
app端接口用例设计方法和测试方法
mysql如何优化
数字孪生应用及意义对电力的主要作用,概念价值。
[机缘参悟-52]:交浅言深要因人而异
Permutation and binary (Ji, DA) (day 84)
768. Block II greed that can complete sorting at most
Digital analog 1232
Unity game, the simplest solution of privacy agreement! Just 3 lines of code! (Reprinted)
J-3-point practice in the second game of 2022 Niuke multi school
Deep learning vocabulary embedded, beam search
《稻盛和夫给年轻人的忠告》阅读笔记
Minimum ticket price (day 80)
Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
Design method and test method of APP interface use case
复盘:DFS与BFS的主要区别,在思想上的区别,代码实现上的区别
C语言力扣第43题之字符串相乘。优化竖式
Add support for @data add-on in idea