当前位置:网站首页>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;
}
};
边栏推荐
- 【笔记】常见组合滤波电路
- Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- LeetCode刷题
- Development of a horse tourism website (realization of login, registration and exit function)
- RPA cloud computer, let RPA out of the box with unlimited computing power?
- Sqlite数据库存储目录结构邻接表的实现2-目录树的构建
- SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
- What is load balancing? How does DNS achieve load balancing?
- Basic mode of service mesh
猜你喜欢
Course of causality, taught by Jonas Peters, University of Copenhagen
基于卷积神经网络的恶意软件检测方法
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
【测试面试题】页面很卡的原因分析及解决方案
51与蓝牙模块通讯,51驱动蓝牙APP点灯
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
爬虫实战(八):爬表情包
【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
Service Mesh介绍,Istio概述
随机推荐
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
国外众测之密码找回漏洞
Lecture 1: the entry node of the link in the linked list
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
If an exception is thrown in the constructor, the best way is to prevent memory leakage?
3年经验,面试测试岗20K都拿不到了吗?这么坑?
Is it safe to open an account on the official website of Huatai Securities?
赞!idea 如何单窗口打开多个项目?
Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
Basic principle and usage of dynamic library, -fpic option context
Zhou Hongqi, 52 ans, est - il encore jeune?
Hotel
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
炒股开户怎么最方便,手机上开户安全吗
Reptile practice (VIII): reptile expression pack