当前位置:网站首页>Letcode43: string multiplication
Letcode43: string multiplication
2022-07-08 00:49:00 【New Young】
Preface :
subject
letcode:https://leetcode.cn/problems/multiply-strings/description/

Ideas

Code
// Ideas :
// Store the result of each multiplication in the array
// And
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)
{
// As a result, it is impossible to fill the array , This is an invisible explanation
// Because the starting data header of each array is different , So use begin Let's start
int gap = 0;// Carry device
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)// The number of result digits is greater than 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;
}
// The number of arrays depends on the shortest length string
int** arr = new int* [min];
// The number of digits of the result of multiplying multiple numbers cannot be greater than the sum of digits of each number , Therefore, the length of each array is len1+len2;
for (size_t i = 0; i < min; ++i)
{
arr[i] = new int[len];
memset(arr[i], 0, sizeof(int) * (len));// The initialization space is 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;// Carry device
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;
}
};
边栏推荐
- Analysis of 8 classic C language pointer written test questions
- Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
- A brief history of information by James Gleick
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- 【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
- 大二级分类产品页权重低,不收录怎么办?
- Operating system principle --- summary of interview knowledge points
- redis你到底懂不懂之list
- Which securities company has a low, safe and reliable account opening commission
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
猜你喜欢

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

3 years of experience, can't you get 20K for the interview and test post? Such a hole?

Interface test advanced interface script use - apipost (pre / post execution script)

【测试面试题】页面很卡的原因分析及解决方案

SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)

备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log
![[研发人员必备]paddle 如何制作自己的数据集,并显示。](/img/50/3d826186b563069fd8d433e8feefc4.png)
[研发人员必备]paddle 如何制作自己的数据集,并显示。

5G NR 系统消息

玩转Sonar

他们齐聚 2022 ECUG Con,只为「中国技术力量」
随机推荐
RPA cloud computer, let RPA out of the box with unlimited computing power?
Introduction to paddle - using lenet to realize image classification method I in MNIST
Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
Codeforces Round #804 (Div. 2)(A~D)
Kubernetes static pod (static POD)
How can CSDN indent the first line of a paragraph by 2 characters?
詹姆斯·格雷克《信息简史》读后感记录
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
Cause analysis and solution of too laggy page of [test interview questions]
Four stages of sand table deduction in attack and defense drill
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
paddle入门-使用LeNet在MNIST实现图像分类方法二
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
攻防演练中沙盘推演的4个阶段
Stock account opening is free of charge. Is it safe to open an account on your mobile phone
Service Mesh介绍,Istio概述
Course of causality, taught by Jonas Peters, University of Copenhagen
韦东山第二期课程内容概要
tourist的NTT模板
【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice