当前位置:网站首页>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;
}
};
边栏推荐
- [programming questions] [scratch Level 2] March 2019 garbage classification
- redis你到底懂不懂之list
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- Operating system principle --- summary of interview knowledge points
- SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
- [研发人员必备]paddle 如何制作自己的数据集,并显示。
- Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
- Summary of the third course of weidongshan
- ABAP ALV LVC模板
- 爬虫实战(八):爬表情包
猜你喜欢

《因果性Causality》教程,哥本哈根大学Jonas Peters讲授

去了字节跳动,才知道年薪 40w 的测试工程师有这么多?

备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log
![[OBS] the official configuration is use_ GPU_ Priority effect is true](/img/df/772028e44776bd667e814989e8b09c.png)
[OBS] the official configuration is use_ GPU_ Priority effect is true

基于人脸识别实现课堂抬头率检测

An error is reported during the process of setting up ADG. Rman-03009 ora-03113

QT establish signal slots between different classes and transfer parameters

v-for遍历元素样式失效

大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?

RPA云电脑,让RPA开箱即用算力无限?
随机推荐
股票开户免费办理佣金最低的券商,手机上开户安全吗
An error is reported during the process of setting up ADG. Rman-03009 ora-03113
Flask learning record 000: error summary
他们齐聚 2022 ECUG Con,只为「中国技术力量」
詹姆斯·格雷克《信息简史》读后感记录
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
tourist的NTT模板
The underlying principles and templates of new and delete
Summary of weidongshan phase II course content
Hotel
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
Four stages of sand table deduction in attack and defense drill
Codeforces Round #804 (Div. 2)(A~D)
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
Sqlite数据库存储目录结构邻接表的实现2-目录树的构建
CVE-2022-28346:Django SQL注入漏洞
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Reentrantlock fair lock source code Chapter 0