当前位置:网站首页>【43. 字符串相乘】
【43. 字符串相乘】
2022-08-11 07:13:00 【安河桥畔】
字符串相加
题目来源
力扣(LeetCode): 字符串相乘
题目描述
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。
示例1
输入
num1 = “2”, num2 = “3”
输出
“6”
示例2
输入
num1 = “123”, num2 = “456”
输出
“56088”
提示
- 1 <= num1.length, num2.length <= 200
- num1 和 num2 只能由数字组成。
- num1 和 num2 都不包含任何前导零,除了数字0本身。
思路分析
- 排除有’0’的情况
- 定义一个字符串保存乘积结果,这个串的长度是两个乘数长度之和
- 两层循环,从低位到高位开始相乘,这里必须将两个数全部转换为int类型相乘,定义临时变量保存乘积
- ▲用k表示当前乘积要保存在的位置,如果有进位则将进位结果保存在k-1的位置,由于char类型范围较小,所以乘积不能直接与ret当前位置的值相加,否则有可能超出char类型取值范围,如:得到乘积81若与’0’相加超出char类型范围。正确的处理方式:先将乘积结果进位保留个位与ret当前位相加,并再次处理加和的进位
- 结果放入字符串中时要转换成char类型,因为字符串每个位置都被初始化为了’0’,所以只需要将乘积加到对应位置即可
- 得到结果后,去除最高位的’0’
代码展示
class Solution {
public:
string multiply(string num1, string num2) {
//有一个乘数为0则积为0
if (num1 == "0" || num2 == "0")
{
return "0";
}
//找较长的串作左操作数(个人习惯,这步可以省略)
if (num1.size() < num2.size())
{
num1.swap(num2);
}
int LeftSize = num1.size();
int RightSize = num2.size();
//保存乘积的字符串,两个数乘积最大位数为两个数位数之和
int RetSize = LeftSize + RightSize;
string ret(RetSize, '0');
int m = 1;//m标记保存结果数据的起始位
for (int i = RightSize - 1; i >= 0; i--)
{
int k = RetSize - m;
for (int j = LeftSize - 1; j >= 0; j--)
{
//定义临时变量保存乘积,int类型
int temp = (num2[i] - '0') * (num1[j] - '0');
//进位,因为乘积最大位81,加上'0'会超出char类型范围,所以要分两次处理进位
ret[k - 1] += temp / 10;
temp %= 10;
ret[k] += temp;
//临时变量temp加到ret字符串中后还要再次处理进位
while (ret[k] > '9')
{
ret[k] -= 10;
ret[k - 1] += 1;
}
k--;//ret中保存结果的位置左移
}
m++;//ret中保存结果的起始位置左移一位
}
//去除最高位的'0'
int count = 0;
for (auto ch : ret)
{
if (ch != '0')
{
break;
}
count++;
}
ret.erase(0, count);
return ret;
}
};
总结
难点及易错点:
- 忽略有’0’的情况
- 两次处理进位结果
- 对于int类型,可以用“除10”和“模10”处理进位
- 保存结果的字符串位置的偏移,内层循环每次结束左移一位,外层循环每轮结束将起始位置左移一位
- 去除结果最高位的’0’
边栏推荐
- Pico neo3 Unity打包设置
- 租房小程序
- Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
- Write a resume like this, easy to get the interviewer
- XXL-JOB 分布式任务调度中心搭建
- Two startup methods and differences of Service
- 为什么我使用C#操作MySQL进行中文查询失败
- 【latex异常和错误】Missing $ inserted.<inserted text>You can‘t use \spacefactor in math mode.输出文本要注意特殊字符的转义
- 1061 True or False (15 points)
- 2.1-梯度下降
猜你喜欢

Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios

1071 Small Gamble (15 points)

1106 2019 Sequence (15 points)

tf中矩阵乘法
1.2 - error sources

Project 1 - PM2.5 Forecast

Tensorflow中使用tf.argmax返回张量沿指定维度最大值的索引

机器学习总结(二)
3.1-Classification-probabilistic generative model

TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
随机推荐
2022 China Soft Drink Market Insights
关于Excel实现分组求和最全文档
There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
Four states of Activity
1.1-回归
1081 检查密码 (15 分)
1096 大美数 (15 分)
MindManager2022全新正式免费思维导图更新
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
Pico neo3在Unity中的交互操作
【LeetCode】链表题解汇总
1.2 - error sources
囍楽cloud task source code
Tf中的平方,多次方,开方计算
我的创作纪念日丨感恩这365天来有你相伴,不忘初心,各自精彩
linux 安装mysql服务报错
The softmax function is used in TF;
接口测试的基础流程和用例设计方法你知道吗?
1061 True or False (15 points)
The easiest trick to support quick renaming of various files