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;
}
};

最新文章

  1. ip命令和ifconfig命令(转载)
  2. 数据格式json讲解
  3. redis 原子增一的妙用
  4. DragSelectRecyclerView 长按滑动多选图像android特效
  5. Android基础:Activity
  6. URL List
  7. 局域网怎么通过IP查看对方MAC
  8. [HIHO1051]补提交卡(枚举,贪心)
  9. 如何用Python脚本从文件读取数据?
  10. echarts (geo/map) 渐变效果
  11. 【学习】C++多态机制
  12. base标签浏览器兼容问题
  13. C#之实现Scoket心跳机制
  14. mysql 初始密码、修改密码
  15. 链栈的基本操作(C语言)
  16. IDEA快捷键--与Eclipse快捷键的对比--IDEA快捷键设置成Eclipse默认快捷键
  17. 百度上传插件(webupload)单文件(单图片)上传设置
  18. MapReduce 过程详解 (用WordCount作为例子)
  19. 通过FISH和下一代测序检测肺腺癌ALK基因融合比较
  20. PHP与Java集成开发详解(一)

热门文章

  1. 动态规划:HDU1224-Free DIY Tour
  2. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
  3. 小米r3g旧版开发版固件,安装opkg
  4. export、export default、module.export区别
  5. JS空数组的判断
  6. Python+Selenium框架设计篇之-什么是自动化测试框架
  7. 数据结构之c++感悟
  8. 贪吃蛇—C—基于easyx图形库(上):基本控制函数实现 画图程序
  9. Java和C#中神奇的String
  10. 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)