130 - 43 字符串相乘

题目

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

示例 1:

输入: num1 = "2", num2 = "3" 输出: "6"

示例 2:

输入: num1 = "123", num2 = "456" 输出: "56088"

说明:

  1. num1 和 num2 的长度小于110。

  2. num1 和 num2 只包含数字 0-9。

  3. num1 和 num2 均不以零开头,除非是数字 0 本身。

  4. 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。

解答

第一想法就是转换,然后第四条说不行哈哈😂

看到了一个巨强的解法:

https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation

之前的想法是,按照数学上的乘法来计算。但完全可以一个个相乘的值再一个个相加

Multiplication

Runtime: 68 ms, faster than 82.58% of JavaScript online submissions for Multiply Strings.

Memory Usage: 35.8 MB, less than 25.00% of JavaScript online submissions for Multiply Strings.

Runtime: 4 ms, faster than 54.02% of Go online submissions for Multiply Strings.

Memory Usage: 3 MB, less than 100.00% of Go online submissions for Multiply Strings.

Runtime: 168 ms, faster than 33.79% of Python3 online submissions for Multiply Strings.

Memory Usage: 13.7 MB, less than 7.14% of Python3 online submissions for Multiply Strings.

Last updated

Was this helpful?