Product of Array Except Self #41
Labels
category:leetcode
to note some leetcode question
language:English
blog written by Engish
tag:javascript
something about javascript
Question
Given an integer array
nums
, return an arrayanswer
such thatanswer[i]
is equal to the product of all the elements ofnums
exceptnums[i]
.The product of any prefix or suffix of
nums
is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in
O(n)
time and without using the division operation.Example
The question is not that difficult if there are no limit conditions, but since it need to be run in a
O(n)
time and and without using the division operation. I tried but didn't get a good answer.And here is a good example I found. (It even has an
O(1)
Space Complexity!!)You may not understand what this approach is doing just like I did first, but if you try to trace the operation you will understand it and find it is a great way to solve the problem.
The mechanism is very simple, which is that the each
answer[i]
is an product value accumulated start from the two side(the first and the lastnum
) of the array, to the twonum
next to thenum[i]
itself.The text was updated successfully, but these errors were encountered: