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. CSS 社区的解决方案,对比

    在众多解决方案中,没有绝对的优劣.还是要结合自己的场景来决定. 我们团队在使用过 scss 和 css modules 后,仍然又重新选择了使用 scss.css modules 虽然有效解决了样式冲 ...

  2. kafka原理和实践(四)spring-kafka消费者源码

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  3. Spring生态研习【二】:SpEL(Spring Expression Language)

    1. SpEL功能简介 它是spring生态里面的一个功能强大的描述语言,支在在运行期间对象图里面的数据查询和数据操作.语法和标准的EL一样,但是支持一些额外的功能特性,最显著的就是方法调用以及基本字 ...

  4. Django2.0资料

    The Django Book 2.0 中文版:点击下载 Django课件和代码:点击下载

  5. spingMVC+mybatis+spring-session共享内存配置

    1. redis依赖: <dependency> <groupId>org.springframework.session</groupId> <artifa ...

  6. 一些有用的Java学习资料

    Better Java,一些好的Java实践 Google Java Style Guide 30个Java编程技巧 JDK8新增语法特性简介,对Java8中新增的函数接口.Lambda表达式.方法引 ...

  7. HTTP Request & Response

    Request & Response header details can be found here The request method indicates the method to b ...

  8. flutter环境配置

    java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...

  9. [转][Echarts]俄罗斯方块

    app.title = '俄罗斯方块'; var refreshT,fallBlockT; var fallTimout; var speed = 1000, downSpeed = 30, nomr ...

  10. C++Primer第五版——习题答案详解(七)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...