当前位置:网站首页>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;
}边栏推荐
- 如何进行 360 评估
- Banyan data model of Bairong
- opiodr aborting process unknown ospid (21745) as a result of ORA-609
- The diagram of user login verification process is well written!
- A new paradigm of distributed deep learning programming: Global tensor
- The application and significance of digital twins are the main role and conceptual value of electric power.
- 基于OpenCV的轮廓检测(1)
- Explain tool actual operation
- 768. 最多能完成排序的块 II 贪心
- Insert pictures and videos in typera
猜你喜欢

基于OpenCV的轮廓检测(2)

网络安全/渗透测试工具AWVS14.9下载/使用教程/安装教程

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

spark学习笔记(五)——sparkcore核心编程-RDD转换算子

【树链剖分】2022杭电多校2 1001 Static Query on Tree

How to optimize MySQL

数据库概论 - 数据库的介绍

The function and application of lpci-252 universal PCI interface can card

Source code analysis of openfeign

Mysql database related operations
随机推荐
Take you to know what Web3.0 is
OC message mechanism
spark学习笔记(五)——sparkcore核心编程-RDD转换算子
How to uniquely identify a user SQL in Youxuan database cluster
Docker creates MySQL 8.x container and supports Mac and arm architecture chips
It's too strong. An annotation handles the data desensitization returned by the interface
MySQL的数据库有关操作
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
常见弱口令大全
Method of converting curtain article OPML into markdown
Typescript TS basic knowledge interface, generics
Kettle读取按行分割的文件
Quick sequencing and optimization
Explain tool actual operation
【无标题】JDBC连接数据库读超时
深入理解Mysql索引底层数据结构与算法
Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation
Redis spike case, learn from Shang Silicon Valley teacher in station B
flask_restful中reqparse解析器继承
[untitled] JDBC connection database read timeout