Description
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:
1 | Input: num1 = "2", num2 = "3" |
Example 2:
1 | Input: num1 = "123", num2 = "456" |
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contain only digits0-9
. - Both
num1
andnum2
do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
解法
其实就是大数相乘的算法,这里需要注意的是在每次做乘法运算时,不考虑进位问题,直接把结果存入结果数组的对应位上,最后统一处理进位问题。注意,计算从数组的尾部开始。
具体代码如下:
1 | class Solution { |