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. 1.1.18 Word里将“字体加红”自定义为快捷方式

    1.单击[文件]>[选项]: 2.选择[自定义功能区],单击[键盘快捷方式]旁的[自定义]: 3.在[类别]菜单中选择[开始选项卡]:在[命令]菜单中选择[FontColorPicker] (或 ...

  2. Guava 6:Concurrency

    一.引子 有点经验的工程师一定对多线程比较熟悉,JDK封装的FutureTask实现了这一功能.如下图: FutureTask实现了RunnableFuture接口,而RunnableFuture接口 ...

  3. Aria2+百度网盘 无限制的下载神器

    Aria2是一款免费开源跨平台且不限速的多线程下载软件,Aria2的优点是速度快.体积小.资源占用少:支持 HTTP / FTP / BT / Magnet 磁力链接等类型的文件下载:支持 Win.M ...

  4. php调用API支付接口 可个人使用,无需营业执照(使用第三方接口,调用的天工接口。)

    首先访问  https://charging.teegon.com/  注册账号, 找到开发配置   记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击  S ...

  5. 洛谷P1636学画画

    传送 这个题我们需要一个大胆的想法(虽然AC后看了题解知道这是个定理) (求证明qwq) 如果一个图有2或0个奇点,它就一定可以一笔画出,如果不是2或0个奇点,那答案就是奇点数/2 (私认为因为两个奇 ...

  6. Python撰写mail

    版本1   指定邮箱进行发送 """ 说明:指定账户密码进行邮件发送 由312051952@qq.com-->c4kaichen@163.com "&qu ...

  7. html5 + shiro

    偶然与巧合 舞动了蝶翼 谁的心头风起 前赴而后继 万千人追寻 荒漠唯一菩提 似擦肩相遇 或擦肩而去 命运犹如险棋 无数时间线 无数可能性 终于交织向你

  8. linux git clone 指定分支

    git clone -b develop http://192.168.11.11:8888/scm/git/vrmmo 指定下载develop分支

  9. python3文件的读写操作

    open函数:对文件进行读写操作前,先打开文件,获取文件的句柄: open(file, mode, encoding, buffering) 参数说明 file_name:一个包含了你要访问的文件路径 ...

  10. springboot常见 10问

    1.什么是Spring Boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有 ...