当前位置:网站首页>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;
}
};
边栏推荐
- Codeforces Round #804 (Div. 2)(A~D)
- “一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
- 语义分割模型库segmentation_models_pytorch的详细使用介绍
- They gathered at the 2022 ecug con just for "China's technological power"
- Kubernetes Static Pod (静态Pod)
- CVE-2022-28346:Django SQL注入漏洞
- If an exception is thrown in the constructor, the best way is to prevent memory leakage?
- How to learn a new technology (programming language)
- 【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
- Hotel
猜你喜欢
备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log
Codeforces Round #804 (Div. 2)(A~D)
fabulous! How does idea open multiple projects in a single window?
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
The underlying principles and templates of new and delete
基于人脸识别实现课堂抬头率检测
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
How to insert highlighted code blocks in WPS and word
爬虫实战(八):爬表情包
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
随机推荐
v-for遍历元素样式失效
How to learn a new technology (programming language)
Single machine high concurrency model design
赞!idea 如何单窗口打开多个项目?
哪个券商公司开户佣金低又安全,又靠谱
How can CSDN indent the first line of a paragraph by 2 characters?
Which securities company has a low, safe and reliable account opening commission
国外众测之密码找回漏洞
Password recovery vulnerability of foreign public testing
【笔记】常见组合滤波电路
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
Reentrantlock fair lock source code Chapter 0
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
【测试面试题】页面很卡的原因分析及解决方案
Course of causality, taught by Jonas Peters, University of Copenhagen
Development of a horse tourism website (realization of login, registration and exit function)
Langchao Yunxi distributed database tracing (II) -- source code analysis
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
Service mesh introduction, istio overview
搭建ADG过程中复制报错 RMAN-03009 ORA-03113