Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.

» Solve this problem

[解题思路]
大整数乘法,一位一位往上乘,注意进位的处理即可。此外,注意0的处理

[Code]

1:    string multiply(string num1, string num2) {  2:      // Start typing your C/C++ solution below  3:      // DO NOT write int main() function  4:      if(num1.size() ==0 || num2.size() ==0) return 0;  5:      string res(num1.size()+num2.size()+1, '0');  6:      std::reverse(num1.begin(), num1.end());  7:      std::reverse(num2.begin(), num2.end());  8:      for(int i =0; i < num1.size(); i++)  9:      {  10:        int dig1 = num1[i] -'0';  11:        int carry = 0;  12:        for(int j = 0; j< num2.size(); j++)  13:        {  14:          int dig2 = num2[j] - '0';  15:          int exist = res[i+j] -'0';          16:          res[i+j] = (dig1*dig2+carry+ exist) % 10 +'0';    17:          carry = (dig1*dig2+carry+exist)/10;  18:        }  19:        if(carry >0)  20:        {  21:          res[i+num2.size()] = carry + '0';  22:        }  23:      }  24:      std::reverse(res.begin(), res.end());  25:      int start =0;  26:      while(res[start] =='0' && start < res.size())  27:      {  28:        start++;  29:      }  30:      if(start == res.size()) return "0";  31:      return res.substr(start, res.size()-start);  32:    }  

[Note]
1. Line 25~31, 处理乘法过程中在起始处产生的0。 比如, “9133”, “0”,直接乘的结果是“000000”, 而期待结果是“0”

转载于:https://www.cnblogs.com/codingtmd/archive/2012/12/28/5078984.html

[LeetCode] Multiply Strings 解题报告相关推荐

  1. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  2. Leetcode: Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The ...

  3. 大整数乘法--leetcode Multiply Strings

    大整数乘法 本文转载自http://www.cnblogs.com/TenosDoIt/p/3735309.html 我们在日常的大整数计算中,通常是把它转化为字符型计算.这道题的思路就和我们小学计算 ...

  4. LeetCode Multiply Strings(大整数相乘)

    思路:用笔算的形式就可以了 代码如下: public class Solution {public String multiply(String num1, String num2) {StringB ...

  5. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  6. LeetCode: Add Binary 解题报告

    Add Binary Given two binary strings, return their sum (also a binary string). For example, a = " ...

  7. Leetcode Weekly 188 解题报告

    文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...

  8. [LeetCode]Distinct Subsequences,解题报告

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  9. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

最新文章

  1. 别再用kill -9关闭程序了......
  2. 修改注释里的作者名字
  3. java自带工具_深入了解Java JDK自带工具,包括javac、jar、jstack等,实用~
  4. 逼出的成功,强迫的辉煌
  5. 我的 计算机朋友作文,电脑我的朋友作文
  6. PyTorch 1.5 发布,与 AWS 合作 TorchServe!
  7. 此次边路调整系统推荐射手走哪路_王者荣耀:射手调整前瞻,阿离回归边路!新英雄/皮肤下周上架...
  8. ARouter解析笔记
  9. autojs版本的QQ聊天自动回复机器人源码免费分享,不需要root权限
  10. ASP多媒体视频教程
  11. 用VC GDI+画一颗树
  12. 分布式数据库技术金融应用规范3项标准出炉,巨杉数据库参与制定
  13. feign+hystrix相关超时时间配置问题
  14. Linux -- 如何修改 Linux 虚拟机 SNMP 的团体名
  15. 成功解决ImportError: cannot import name ‘GloVe‘ from ‘torchtext.legacy.vocab‘
  16. 一文给你解决linux内存源码分析- SLUB分配器概述(超详细)
  17. LB10S-ASEMI整流桥LB10S
  18. Three.js实战--修改OrbitControls的按键
  19. 优酷html代码手机不支持,使用 iframe 引用优酷和土豆和腾讯视频,支持 HTML5 手机 播放...
  20. 【笔记】设计心理学 [美] 唐纳德·A·诺曼

热门文章

  1. c++ 指向类的静态成员的指针
  2. Appium+python自动化(十五)- Android 这些基础知识,你知多少???(超详解)...
  3. python-pcl
  4. Oracle数据隐式乱码,正则匹配中文数据失败
  5. Android-04:线程的使用
  6. Linux下c开发 之 线程通信
  7. WEB前端学习四 js什么是原始类型
  8. Jquery实现 全选反选
  9. (四)代码优化 (快来看看怎样写出真正高性能的代码)
  10. x86已安装该产品 剑灵vcredist_MySQL Server v5.7正式版(附安装和配置数据库教程)