当前位置:网站首页>C语言力扣第43题之字符串相乘。优化竖式
C语言力扣第43题之字符串相乘。优化竖式
2022-07-27 02:11:00 【管二狗绝不摆烂】
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。
示例 1:
输入: num1 = "2", num2 = "3"
输出: "6"
示例 2:
输入: num1 = "123", num2 = "456"
输出: "56088"
提示:
1 <= num1.length, num2.length <= 200
num1 和 num2 只能由数字组成。
num1 和 num2 都不包含任何前导零,除了数字0本身。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/multiply-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
使用数组代替字符串存储结果,则可以减少对字符串的操作。
令 m 和 n 分别表示 num1 和 num2 的长度,并且它们均不为 0,则 num1 和 num2 的乘积的长度为 m+n−1或 m+n。简单证明如下:
——如果 num1 和 num2 都取最小值,则 num1=10^{m-1},num2=10^{n-1},num1×num2=10^{m+n-2},乘积的长度为 m+n−1;
——如果 num1 和 num2 都取最大值,则 num1=10^m-1,num2=10^n-1,num1×num2=10^{m+n}-10^m-10^n+1,乘积显然小于 10^{m+n}且大于 10^{m+n-1},因此乘积的长度为 m+n。
由于 num1 和 num2 的乘积的最大长度为 m+n,因此创建长度为 m+n 的数组 ansArr用于存储乘积。对于任意 0≤i<m和 0≤j<n,num1[i]×num2[j] 的结果位于 ansArr[i+j+1],如果 ansArr[i+j+1]≥10,则将进位部分加到 ansArr[i+j]。
最后,将数组 ansArr 转成字符串,如果最高位是 0 则舍弃最高位。
#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;
}边栏推荐
- Leetcode 207. curriculum (July 26, 2022)
- $128million! IQM, a Finnish quantum computing company, was supported by the world fund
- Redis spike case, learn from Shang Silicon Valley teacher in station B
- Reading notes of Kazuo Inamori's advice to young people
- [1206. Design skip table]
- Docker creates MySQL 8.x container and supports Mac and arm architecture chips
- Source code analysis of openfeign
- Practice of online problem feedback module (XV): realize the function of online updating feedback status
- 数据库概论 - MySQL的简单介绍
- mysql底层数据结构
猜你喜欢

数字孪生应用及意义对电力的主要作用,概念价值。

Add support for @data add-on in idea

$128million! IQM, a Finnish quantum computing company, was supported by the world fund

FastBoot brush machine

Indexing best practices

Explain

Explain工具实际操作

mysql底层数据结构

Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical

477-82(236、61、47、74、240、93)
随机推荐
Redis source code learning (33), command execution process
Code practice when the queue reaches the maximum length
PIP3 setting alicloud
768. 最多能完成排序的块 II 贪心
客户案例 | 关注老年用户体验,银行APP适老化改造要避虚就实
The diagram of user login verification process is well written!
768. Block II greed that can complete sorting at most
How can you access the domestic server and overseas server quickly with one database?
数据库概论 - 数据库的介绍
Insert pictures and videos in typera
深入理解Mysql索引底层数据结构与算法
如何进行 360 评估
Explain详解
Deployment of ruoyi's environment and operation of the system
Practice of online problem feedback module (XV): realize the function of online updating feedback status
Database usage security policy
easyui中textbox在光标位置插入内容
Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)
It's too strong. An annotation handles the data desensitization returned by the interface
数字孪生应用及意义对电力的主要作用,概念价值。