LeetCode OJ--Multiply Strings **
2024-09-21 09:18:39
https://oj.leetcode.com/problems/multiply-strings/
用字符串实现大数乘法,细节题,细节很多
class Solution {
public:
string multiply(string num1, string num2) {
if(num1.empty() || num1.size() == || num2.empty() || num2.size() == )
return "";
//
if(num1 == "" || num2 == "")
return ""; //keep num2 the smaller
if(num1.size() < num2.size())
{
//exchange
string t = num1; num1 = num2; num2 = t;
}
string ans = "";
ans.resize(num1.size()); //initialize
for(size_t i = ; i < num1.size(); i++)
ans[i] = ''; int pos = ;
int times = ;
int sum = ; for(int index2 = num2.size() - ; index2 >= ; index2--)
{
pos = ans.size() - - times;
times++;
int carry = ;
for(int index1 = num1.size() - ; index1 >=; index1--)
{
sum = carry + (num1[index1] - '')*(num2[index2] - '') + ans[pos] - '';
carry = sum/;
ans[pos] = sum% + '';
pos--;
// ans has been the beginning while index1 not, and ans has been the beginning and here are carry
if(pos == - &&(carry > || index1 != ))
{
string t = "";
t[] = carry + '';
ans = t + ans;
carry = ;
pos = ;
}
}
}
return ans;
}
};
最新文章
- ip命令和ifconfig命令(转载)
- 数据格式json讲解
- redis 原子增一的妙用
- DragSelectRecyclerView 长按滑动多选图像android特效
- Android基础:Activity
- URL List
- 局域网怎么通过IP查看对方MAC
- [HIHO1051]补提交卡(枚举,贪心)
- 如何用Python脚本从文件读取数据?
- echarts (geo/map) 渐变效果
- 【学习】C++多态机制
- base标签浏览器兼容问题
- C#之实现Scoket心跳机制
- mysql 初始密码、修改密码
- 链栈的基本操作(C语言)
- IDEA快捷键--与Eclipse快捷键的对比--IDEA快捷键设置成Eclipse默认快捷键
- 百度上传插件(webupload)单文件(单图片)上传设置
- MapReduce 过程详解 (用WordCount作为例子)
- 通过FISH和下一代测序检测肺腺癌ALK基因融合比较
- PHP与Java集成开发详解(一)
热门文章
- 动态规划:HDU1224-Free DIY Tour
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
- 小米r3g旧版开发版固件,安装opkg
- export、export default、module.export区别
- JS空数组的判断
- Python+Selenium框架设计篇之-什么是自动化测试框架
- 数据结构之c++感悟
- 贪吃蛇—C—基于easyx图形库(上):基本控制函数实现 画图程序
- Java和C#中神奇的String
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)