当前位置:网站首页>656 · 大整数乘法
656 · 大整数乘法
2022-04-22 19:19:00 【yinhua405】
以字符串的形式给定两个非负整数 num1 和 num2,返回 num1 和 num2 的乘积。
- num1 和 num2 的长度都小于110。
- num1 和 num2 都只包含数字 0 - 9。
- num1 和 num2 都不包含任意前导零。
- 您不能使用任何内置的BigInteger库内方法或直接将输入转换为整数。
样例
样例1
输入:
"123"
"45"
输出:
"5535"
解释:
123 x 45 = 5535
样例2
输入:
"0"
"0"
输出:
"0"
string multiply(string &num1, string &num2) {
// write your code here
int size1 = num1.size();
int size2 = num2.size();
string tmp = "";
if (size1 < size2)
{
tmp = num1;
num1 = num2;
num2 = tmp;
}
size1 = num1.size();
size2 = num2.size();
vector<int> ret(size1 + size2, 0);
for (int j = size2 - 1; j >= 0; j--)
{
for (int i = size1 - 1; i >= 0; i--)
{
int sum = (num2[j] - '0') * (num1[i] - '0');
int p1 = i + j +1 ;
int res = ret[p1] + sum + ret[p1 - 1] *10;
ret[p1-1] = res / 10;
ret[p1] = res % 10;
}
}
string str = "0";
int i = 0;
while (ret[i] == 0)
{
i++;
continue;
}
if (i >= ret.size())
{
str = "0";
}
while (i < ret.size())
{
str += ret[i] + '0';
i++;
}
return str;
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/124336267
边栏推荐
- Will map () in JS change the original array
- 项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图工作前期知识:摄像机)
- 07-Complex Types
- Fingerprint identification record
- Redis的key和value最佳实践
- transfer方法详解
- postgre创建序列并绑定到表字段
- 14 sprak set automatic zoning
- Official component amap of Gaud fluent_ Flutter_ Map draw a circle on the map
- LeetCode_ 343 integer split
猜你喜欢

Linux环境下部署redis教程详解

Where is the cut file? How to recover the lost file cut? Just 3 steps

08-UDFs

ArrayList learning notes

完美转发实现机制

Project training - Design and development of 2D multiplayer fighting game based on unity (v. use audiomixer to control the volume)

webrtc+turn+peerconnection_ Server measurement delay

Pychar configures CONDA and uses the correct image source address in China

Database index

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
随机推荐
VS 2022 安装vld内存泄漏检测工具
mmocr DBLoss
Line by line interpretation of redet code
Can deleted photos be restored? 3 tips to restore deleted photos
08-UDFs
Garbage collector and CMS G1
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图修改完善)
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(三、Unity PlasticSCM多人协同)
C # joint programming with Halcon
leetcode:642. Design search automatic completion system
07-Complex Types
@RequestBody修饰的对象属性大小写映射问题
Understanding of string constant pool and intern method
Flink窗口的类型
【面试普通人VS高手系列】请说一下网络四元组
06. 重构-简化条件表达式
防火门能申请BS 476-21 耐火测试吗?
Misuse of redis cache string
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图工作前期知识:摄像机)
08-UDFs