当前位置:网站首页>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;
}边栏推荐
- 复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些
- Basic concept and essence of Architecture
- 快速排序及优化
- How to conduct 360 assessment
- Explain
- [understanding of opportunity -52]: the depth of communication varies from person to person
- Maximum continuous subsequence (day 77)
- [common search questions] 111
- Tool class of localdatetime sorted out by yourself
- 带你了解什么是 Web3.0
猜你喜欢

客户端发送一条sql如何与服务器交互

Mysql: summary of common sub database and sub table schemes of Internet companies

Introduction to database - Introduction to database
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

Win10/win11 lossless expansion of C disk space, cross disk consolidation of C and e disks

基于OpenCV的轮廓检测(2)

redis入门练习
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

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

Explain详解
随机推荐
数据库概论 - 数据库的介绍
Wechat applet generation Excel
[tree chain dissection] template question
volatile关键字及其作用
MySQL Chinese failure
基于OpenCV的轮廓检测(1)
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
Fastboot刷机
Practical application of digital twins: smart city project construction solution
768. Block II greed that can complete sorting at most
Maximum continuous subsequence (day 77)
Mysql: summary of common sub database and sub table schemes of Internet companies
PIP3 setting alicloud
[1206. Design skip table]
【常用搜索问题】111
复盘:DFS与BFS的主要区别,在思想上的区别,代码实现上的区别
数据库概论 - MySQL的简单介绍
《稻盛和夫给年轻人的忠告》阅读笔记
mysql如何优化
Add support for @data add-on in idea