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. goaccess geoip 测试

      goaccess 是一个很不错的日志实时统计分析工具,我们可以用来方便的分析nginx apcahe iis 等的日志信息 对于geoip 的支持是需要源码编译的,所以基于官方docker 镜像添 ...

  2. InnoDB存储引擎文件

    InnoDB存储引擎文件 MySQL数据库包括数据库本身的文件和存储引擎文件.数据库自身的文件由参数文件(my.cnf).错误日志文件.慢查询日志文件.查询日志文件.二进制日志文件.套接字文件.pid ...

  3. Mysql临时文件目录控制

    查看mysql的log-error日志发现如下错误: ERROR 3 (HY000): Error writing file '/tmp/MYbEd05t' (Errcode: 28) 这是由于mys ...

  4. js EL 正则表达式

    <script> //校验是否全由数字组成20位数 var patrn=/^[0-9]{1,20}$/; alert(patrn.test("-30000000000" ...

  5. Hadoop 权限管理

    Hadoop的权限管理同Linux的很像,有用户,用户组之分,同时Hadoop提供了权限管理命令,主要包括: chmod [-R] mode file … 只有文件的所有者或者超级用户才有权限改变文件 ...

  6. SAS LOGISTIC 逻辑回归中加(EVENT='1')和不加(EVENT='1')区别

    区别在于:最大似然估计分析中估计是刚好正负对调加上EVENT:%LET DVVAR = Y;%LET LOGIT_IN = S.T3;%LET LOGIT_MODEL = S.Model_Params ...

  7. vim中行末去掉^M

    方式1: 输入 :%s/^M//g 方式2: 输入:%s/\r//g

  8. css3属性box-sizing:border-box 用法解析

    响应式Web设计经常需要我们通过百分比设置组件宽度.如果我们不考虑边框,那么很容易就可以实现,但如果你给每一列以及总宽度都采用百分比设置,那这个时候固定的边框大小就会出来捣乱.下面我们将看到一组方法去 ...

  9. REST framwork之分页器,路由器,响应器

    一 REST framwork分页器: from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination ...

  10. FTP管理常用命令

    #新增用户liuhui,指定群组为groupa,附加群组为groupb,家目录为/ftp/groupbuseradd -g groupa -G groupb -d /ftp/groupb linhui ...