当前位置:网站首页>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;
}
};
边栏推荐
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
- 炒股开户怎么最方便,手机上开户安全吗
- ReentrantLock 公平锁源码 第0篇
- 第一讲:链表中环的入口结点
- 手写一个模拟的ReentrantLock
- LeetCode刷题
- Cancel the down arrow of the default style of select and set the default word of select
- 5g NR system messages
- 浪潮云溪分布式数据库 Tracing(二)—— 源码解析
- 52岁的周鸿祎,还年轻吗?
猜你喜欢

"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points

How to insert highlighted code blocks in WPS and word

3年经验,面试测试岗20K都拿不到了吗?这么坑?

【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
![[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game](/img/81/c84432a7d7c2fe8ef377d8c13991d6.png)
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game

【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础

After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?

Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
![[研发人员必备]paddle 如何制作自己的数据集,并显示。](/img/50/3d826186b563069fd8d433e8feefc4.png)
[研发人员必备]paddle 如何制作自己的数据集,并显示。

从服务器到云托管,到底经历了什么?
随机推荐
Jouer sonar
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
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
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Single machine high concurrency model design
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Handwriting a simulated reentrantlock
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
DNS series (I): why does the updated DNS record not take effect?
Smart regulation enters the market, where will meituan and other Internet service platforms go
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
How to insert highlighted code blocks in WPS and word
Flask learning record 000: error summary
Service mesh introduction, istio overview
Is it safe to open an account on the official website of Huatai Securities?
语义分割模型库segmentation_models_pytorch的详细使用介绍