当前位置:网站首页>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;
}
};
边栏推荐
- 玩转Sonar
- ReentrantLock 公平锁源码 第0篇
- Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
- Smart regulation enters the market, where will meituan and other Internet service platforms go
- 牛客基础语法必刷100题之基本类型
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
- 51与蓝牙模块通讯,51驱动蓝牙APP点灯
- 5G NR 系统消息
- Su embedded training - Day3
- 韦东山第二期课程内容概要
猜你喜欢

QT establish signal slots between different classes and transfer parameters

5g NR system messages

Analysis of 8 classic C language pointer written test questions

赞!idea 如何单窗口打开多个项目?

jemter分布式

“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点

51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

FOFA-攻防挑战记录

Installation and configuration of sublime Text3

测试流程不完善,又遇到不积极的开发怎么办?
随机推荐
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Smart regulation enters the market, where will meituan and other Internet service platforms go
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
韦东山第二期课程内容概要
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
FOFA-攻防挑战记录
They gathered at the 2022 ecug con just for "China's technological power"
取消select的默认样式的向下箭头和设置select默认字样
Deep dive kotlin synergy (XXII): flow treatment
国外众测之密码找回漏洞
What is load balancing? How does DNS achieve load balancing?
玩轉Sonar
丸子官网小程序配置教程来了(附详细步骤)
A brief history of information by James Gleick
基于微信小程序开发的我最在行的小游戏
手机上炒股安全么?
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
新库上线 | CnOpenData中华老字号企业名录
fabulous! How does idea open multiple projects in a single window?
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points