43. Multiply Strings
2017-02-09 14:55
218 查看
Given two non-negative integers
Note:
The length of both
Both
Both
You must not use any built-in BigInteger library or convert the inputs to integer directly.
Subscribe to see which companies asked this question.
来源: https://leetcode.com/problems/multiply-strings/
进一步改进因为add函数每一次都要reverse两个string,最后算数结束还需要再次reverse结果,导致效率很低
其实可以将乘法和最后相加的过程整合在一起。这里用到一个条件,两个数(一个n位,一个m位)相乘,则乘积最大长度,不超过n+m位这样的话,建立一个n+m位的string,作为存储乘积结果的字符串result,每次每个数字相乘后,就和result相加
num1: "11223" n = 5num2: "123456" m = 6index: 0 1 2 3 4 5 6 7 8 9 10result: 0 0 0 0 0 0 0 0 0 0 0 当 i = 2; j = 3时,指向的result位置是i+j+1 *
null
num1and
num2represented as strings, return the product of
num1and
num2.
Note:
The length of both
num1and
num2is < 110.
Both
num1and
num2contains only digits
0-9.
Both
num1and
num2does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
Subscribe to see which companies asked this question.
来源: https://leetcode.com/problems/multiply-strings/
分析
与大数相加类似下面的算法是按照正常的算数过程,计算出num1和num2每个位数字的乘积,然后再相加28ms其实可以将乘法和最后相加的过程整合在一起。这里用到一个条件,两个数(一个n位,一个m位)相乘,则乘积最大长度,不超过n+m位这样的话,建立一个n+m位的string,作为存储乘积结果的字符串result,每次每个数字相乘后,就和result相加
num1: "11223" n = 5num2: "123456" m = 6index: 0 1 2 3 4 5 6 7 8 9 10result: 0 0 0 0 0 0 0 0 0 0 0 当 i = 2; j = 3时,指向的result位置是i+j+1 *
相关文章推荐
- leetcode(43) - Multiply Strings
- 43. Multiply Strings
- LeetCode.415(43) Add Strings&&Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 415/43 Add Strings/Multiply Strings
- 43. Multiply Strings
- leetcode || 43、Multiply Strings
- Java [Leetcode 43]Multiply Strings
- leetcode43 Multiply Strings
- 43. Multiply Strings
- Medium 43题 Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- LeetCode43_Multiply Strings
- LeetCode(43) Multiply Strings
- 43 - Multiply Strings
- 43. Multiply Strings