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.

题意:

高精度乘法。

Solution1: Math

Do the simulation like how computer will do multiplication operation

1一个char对应1个digit,

digit相乘后,注意其乘积结果可能需要进位

code

 /*
Time: O(n^2). We use nested 2 for loop
Space: O(n). We use int[] to save intermedia infor
*/ class Solution {
public String multiply(String num1, String num2) {
if(num1.length()==0 || num2.length()==0) return "0";
int len1 = num1.length();
int len2 = num2.length();
int [] result = new int [len1+len2]; for(int i = len1-1; i>=0; i--){
for(int j = len2-1; j>=0;j--){
int mul = (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
int idx = i+j+1;
// 可能会进位
int carryIdx = i+j;
mul = mul + result[idx];
result[idx] = mul % 10;
result[carryIdx] = result[carryIdx] + mul/10;
}
}
StringBuilder sb = new StringBuilder();
for(int res: result){
if(sb.length()!=0 || res!=0) sb.append(res);
}
return (sb.length() == 0)? "0" : sb.toString();
}
}

[leetcode]43. Multiply Strings高精度乘法的更多相关文章

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

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

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

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

  3. [LeetCode] 43. Multiply Strings 字符串相乘

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

  4. LeetCode(43. Multiply Strings)

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

  5. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  6. 43. Multiply Strings (大数乘法)

    DescriptionHintsSubmissionsDiscussSolution   Pick One Given two non-negative integers num1 and num2  ...

  7. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  8. leetcode 43 Multiply Strings 大数相乘

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

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

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

随机推荐

  1. tomcat 端口修改和内存配置

    端口号修改参考:https://jingyan.baidu.com/article/adc815139b12def722bf7377.html Tomcat内存溢出(windows) java.lan ...

  2. 第2章 Java基本语法(上): 变量与运算符

    2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...

  3. JDK 8 安装及配置

    1.配置java环境变量 注意:jdk文件夹名字取名不要用汉语取名. 1)鼠标右键点击我的电脑(计算机)选择属性栏 2)再点击左边高级系统设置 3)点击环境变量 4)在用户变量窗口新建变量名为JAVA ...

  4. webpack 中,module、chunk、bundle 的区别(待补充)

    项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle

  5. C#中Button.DialogResult属性

    窗体中的某个按钮,如果设置了DialogResult(不是设置为None),当窗体是通过ShowDialog方法显示的时候 则不必设置任何响应函数,单击按钮也可窗体.然后,该窗体的DialogResu ...

  6. .net updatePannel 局部刷新效果实现后,但是仍是全部刷新的修改方法

    最近做了一个小例子,就是晚上都有的那种小的updatepannel的局部刷新的小例子,但是发现按照那个例子虽然能够实现label2的局部刷新,但是看上去效果确实整个页面都在刷新,这让人很头疼,所以我在 ...

  7. Ubuntu 16.04 LTS 常用快捷键

    在Linux下Win键就是Super键 启动器 Win(长按) 打开启动器,显示快捷键 Win + Tab 通过启动器切换应用程序 Win + 1到9 与点击启动器上的图标效果一样 Win + Shi ...

  8. # 20175227 2018-2019-2 《Java程序设计》第一周学习总结

    20175227 2018-2019-2 <Java程序设计>第一周学习总结 教材学习内容总结 1.安装VB,Ubuntu,Git,JDK,并自行配置. 2.写"Hello Wo ...

  9. 使用Java监控工具出现 Can't attach to the process

    问题重现 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 ➜ jinfo -flags 3032 Attaching ...

  10. WPF 选项卡

    1.引用 xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock" 2.xaml代码 <xcad:DockingMa ...