Multiply Two Integers
Given two integersnum1
andnum2
represented as lists, return the product ofnum1
andnum2
, also represented as a list. num1
andnum2
maybe negative if the leading digit is negative.
Example 1:
Example 2:
Note:
Both
num1
andnum2
contain only digits0-9
.Both
num1
andnum2
do not contain any leading zero, except the number 0 itself.You must not convert the inputs to integer directly.
Solution
Idea:
The number of digits required for the product is at most for and digit operands. So we can initialize the result as an array for multiplication and remove all leading zeros at the final step.
Simulate the integer multiplication algorithm.
Time complexity: , where are length of inputs.
Last updated