当前位置:网站首页>letcode43:字符串相乘
letcode43:字符串相乘
2022-07-07 22:58:00 【New Young】
前言:
题目
letcode:https://leetcode.cn/problems/multiply-strings/description/
思路
代码
//思路:
//用每位相乘的结果存放到数组中即可
//且
void reverseString(string& s) {
size_t i = 0;
size_t j = s.size() - 1;
for (; i < j; ++i, --j)
{
std::swap(s[i], s[j]);
}
}
void Sum(int* arr, int begin, const string& num, const char& ch)
{
//结果是不可能把数组填满的,这是隐形的说明
//因为每个数组的启始数据头是不同的,所以用begin来开始
int gap = 0;//进位器
int len = num.size();
int i = 0;
int j = len - 1;
while (i < len && j >= 0)
{
int sum = (num[j] - '0') * (ch - '0') + gap;
gap = sum / 10;
arr[begin - i] = sum % 10;
++i;
--j;
}
if (gap != 0)//结果位数大于len
{
arr[begin - i] = gap;
++i;
}
}
class Solution {
public:
string multiply(string num1, string num2) {
if ((strcmp(num1.c_str(), "0") == 0)
|| (strcmp(num2.c_str(), "0") == 0))
{
string s = "0";
return s;
}
size_t len1 = num1.size();
size_t len2 = num2.size();
size_t len = len1 + len2;
size_t min = len1;
if (len1 > len2)
{
min = len2;
}
//数组的个数取决于最短长度的string
int** arr = new int* [min];
//多位数相乘的结果位数不可能大于每位数的数位之和,因此每个数组长度取len1+len2;
for (size_t i = 0; i < min; ++i)
{
arr[i] = new int[len];
memset(arr[i], 0, sizeof(int) * (len));//初始化空间为0
}
if (min == len2)
{
for (size_t i = 0; i < min; ++i)
{
Sum(arr[i], len - 1 - i, num1, num2[len2 -1- i]);
}
}
else
{
for (size_t i = 0; i < min; ++i)
{
Sum(arr[i], len - 1 - i, num2, num1[len1 - 1 - i]);
}
}
int gap = 0;//进位器
int* tmp = new int[len];
memset(tmp, 0, sizeof(int) * (len));
for (int i = len - 1; i >= 0; --i)
{
int sum = gap;
for (int j = 0; j < min; ++j)
{
sum += arr[j][i];
}
gap = sum / 10;
tmp[i] = sum % 10;
}
int i = 0;
string s;
while (i < len)
{
if (tmp[i] != 0)
{
for (int j = i; j < len; ++j)
{
s += tmp[j] + '0';
}
break;
}
++i;
}
return s;
}
};
边栏推荐
- paddle入门-使用LeNet在MNIST实现图像分类方法二
- A brief history of information by James Gleick
- 股票开户免费办理佣金最低的券商,手机上开户安全吗
- Su embedded training - Day3
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
- Summary of weidongshan phase II course content
- Lecture 1: the entry node of the link in the linked list
- The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
- Notice on organizing the second round of the Southwest Division (Sichuan) of the 2021-2022 National Youth electronic information intelligent innovation competition
猜你喜欢
玩转Sonar
Kubernetes Static Pod (静态Pod)
国外众测之密码找回漏洞
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
基于卷积神经网络的恶意软件检测方法
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
Reptile practice (VIII): reptile expression pack
语义分割模型库segmentation_models_pytorch的详细使用介绍
CVE-2022-28346:Django SQL注入漏洞
随机推荐
How to insert highlighted code blocks in WPS and word
去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
Summary of the third course of weidongshan
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
NVIDIA Jetson测试安装yolox过程记录
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
A brief history of information by James Gleick
fabulous! How does idea open multiple projects in a single window?
Play sonar
接口测试进阶接口脚本使用—apipost(预/后执行脚本)
Kubernetes Static Pod (静态Pod)
If an exception is thrown in the constructor, the best way is to prevent memory leakage?
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
Su embedded training - Day3
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
How to learn a new technology (programming language)
Notice on organizing the second round of the Southwest Division (Sichuan) of the 2021-2022 National Youth electronic information intelligent innovation competition
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up