Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

 class Solution {
public String multiply(String num1, String num2) {
int m = num1.length(), n = num2.length();
int[] pos = new int[m + n]; for(int i = m - 1; i >= 0; i--) {
for(int j = n - 1; j >= 0; j--) {
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
int p1 = i + j, p2 = i + j + 1;
int sum = mul + pos[p2]; pos[p1] += sum / 10;
pos[p2] = (sum) % 10;
}
} StringBuilder sb = new StringBuilder();
for(int p : pos)
if(!(sb.length() == 0 && p == 0))
sb.append(p);
return sb.length() == 0 ? "0" : sb.toString(); }
}

43. Multiply Strings (大数乘法)的更多相关文章

  1. [leetcode]43. Multiply Strings高精度乘法

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  2. leetcode 43. Multiply Strings(高精度乘法)

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

  3. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

  4. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  5. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  9. 43. Multiply Strings 字符串表示的大数乘法

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

随机推荐

  1. Delphi maskedit控件的掩码含义及用法方法

    Delphi maskedit控件的掩码含义及用法方法   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  2. ansible--我的几个报错

    我的几个报错: 1.远程复制失败 [root@localhost ~ ]#scp -r .ssh 192.168.10.145:/root/ root@192.168.10.145's passwor ...

  3. 【Python全栈-jQuery】jQuery基础知识

    前端学习之jQuery 一. jQuery是什么? <1> jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. < ...

  4. Servlet----------通过 HttpServlet 开发Servlet

    通过继承HttpServlet抽象类,功能更强大. 通过HttpServlet方法开发Servlet需要重写doGet和doPost方法.这是目前用的最多的一种方法. 如:   class MyHtt ...

  5. python的前后端分离(一):django+原生js实现get请求

    一.django新建项目和应用 参考:https://segmentfault.com/a/1190000016049962#articleHeader2 1.创建项目 django-admin st ...

  6. shell分析日志常用指令合集

    数据分析对于网站运营人员是个非常重要的技能,日志分析是其中的一个.日志分析可以用专门的工具进行分析,也可以用原生的shell脚本执行,下面就随ytkah看看shell分析日志常用指令有哪些吧.(log ...

  7. 如何在ASP.NET 网站项目中使用C# 6?

    作者:Generic链接:https://www.zhihu.com/question/48864375/answer/113316462来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  8. 算法面经之讯飞+CVTE

    一.科大讯飞(合肥) 概况:刚经历了科大讯飞的初面,大概35分钟左右,问的内容比较笼统,主要针对简历上的内容来,面试官比较亲切,回忆了一下面试内容. 建议:把简历上的内容整吧清楚,不知道的别瞎写,写了 ...

  9. MSDN、OEM、VOL、RETAIL密钥区别

    本文就介绍一下Windows的密钥的一些使用要点及注意事项,涉及到最常用的MSDN密钥.OEM密钥.VOL密钥和零售密钥激活问题,希望对大家有所帮助. 一.MSDN密钥 MSDN密钥是付费用户提前获得 ...

  10. element-table中使用checkbox

    <el-table-column label="> <template slot-scope="scope"> <el-checkbox v ...