当前位置:网站首页>【LeetCode】43. 字符串相乘
【LeetCode】43. 字符串相乘
2022-07-01 15:42:00 【Uaena_An】
43. 字符串相乘

🧸读题
给两个字符串,相乘后得到一个字符串,这个题不能用整形做,因为后面的测试用例会很大,整形,长整形都装不行!
🧸思路
要按位相乘,存进字符串,再将乘积的得到的字符串相加,最终结果再依次存进字符串内,
🧸代码
class Solution {
public:
string addStrings(string& count, string& sum)
{
//end1,end2从两数尾部相加,carry记录进位
int end1 = count.size() - 1, end2 = sum.size() - 1, carry = 0;
string ret;//记录两数相加的和
while (end1 >= 0 || end2 >= 0)
{
//如果end1大于0 则说明count还有为数,则x1取end1位的值,否则取0
int x1 = 0;
if (end1 >= 0)
{
x1 = count[end1] - '0';
--end1;
}
int x2 = 0;
if (end2 >= 0)
{
x2 = sum[end2] - '0';
--end2;
}
//add记录两数相加的和 ,加上进位,第一次进位是0
int add = x1 + x2 + carry;
//如果add大于9,则说明需要进位,并且add要减去10
//并且进位置1 ,否则进位置0,不然的话进位会保留上次进位的结果,会出现错误
if (add > 9)
{
carry = 1;
add -= 10;
}
else {
carry = 0;
}
//将进位所剩的字符尾插到 ret中,后面再翻转,以提高效率
ret += add;
}
//走到这,说明两数都已经加完了,但是进位还有1位,要尾插进去
if (carry != 0)
{
ret += carry;
carry = 0;
}
//翻转
reverse(ret.begin(), ret.end());
//转成数字字符
for (auto& e : ret)
{
e += '0';
}
return ret;
}
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0")
{
return "0";
}
string count = "0";//记录总和
int end1 = num1.size() - 1, end2 = num2.size() - 1;
//按位相乘,num2每一位 * num1所有位
for (int i = end2; i >= 0; --i)
{
string sum;//记录乘积
int carry = 0;//记录进位
//当j = i时 ,说明j在各位。是最后一位相乘,不需要+0
//当j--后 j在十位上,十位上的数乘其他数,直接在后面+1个0,以此类推
for (int j = end2; j > i; --j)
{
sum.push_back(0);
}
//i控制的是num2倒叙遍历的每一位
int x2 = num2[i] - '0';
//下面是用x2 * num1每一位
for (int j = end1; j >= 0; --j)
{
int x1 = num1[j] - '0';
int ret = x1 * x2 + carry;//两数相乘+进位
sum += ret % 10;//sum存个位 ,尾插
carry = ret / 10;//carry存十位
}
//当两数乘完 进位还有数字,则需要将 进位尾插
while (carry != 0) {
sum += carry % 10;
carry /= 10;
}
//尾插后 翻转字符串
reverse(sum.begin(), sum.end());
//将字符串转化成数字形式的字符串
for (auto &e : sum) {
e += '0';
}
count = addStrings(count, sum);
}
return count;
}
};
🧸解读代码
解读代码已经写在代码注释里了!
胡思乱想
这个题做了2个小时,看了题解 ,一步一步看懂题解,然后尝试写了一下,也算理解了一半,以后还要再做做几遍
加油 祝你拿到心仪的offer!
边栏推荐
- Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]
- swiper 轮播图,最后一张图与第一张图无缝衔接
- MySQL高级篇4
- An intrusion detection model
- For the sustainable development of software testing, we must learn to knock code?
- 点云重建方法汇总一(PCL-CGAL)
- Photoshop插件-HDR(二)-脚本开发-PS插件
- SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
- Returning to the top of the list, the ID is still weak
- Rhcsa fourth day operation
猜你喜欢

【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集
![[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)](/img/0a/c1a4b57b9729e0cf9de1feae9f8c19.png)
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)

《QT+PCL第九章》点云重建系列2

《QT+PCL第六章》点云配准icp系列2

点云重建方法汇总一(PCL-CGAL)

TensorFlow团队:我们没被抛弃

C#/VB.NET 合并PDF文档

An intrusion detection model

Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题

异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
随机推荐
For the sustainable development of software testing, we must learn to knock code?
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
Redis秒杀demo
Tensorflow team: we haven't been abandoned
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
STM32ADC模拟/数字转换详解
Recommendation of data acquisition tools and detailed graphic process of data acquisition list
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]
Using swiper to make mobile phone rotation map
Qt+pcl Chapter 6 point cloud registration ICP series 4
phpcms后台上传图片按钮无法点击
Reading notes of top performance version 2 (V) -- file system monitoring
最新NLP赛事实践总结!
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
采集数据工具推荐,以及采集数据列表详细图解流程
[target tracking] |stark
Summary of point cloud reconstruction methods I (pcl-cgal)
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
做空蔚来的灰熊,以“碰瓷”中概股为生?
《QT+PCL第六章》点云配准icp系列2