Product Except Self
MediumArrays
Given an array nums, return an array answer where answer[i] is the product of every number in nums except nums[i].
Do it in O(n) time and without division (a zero would break the divide-the-total trick anyway).
Constraints
- 2 ≤ length of
nums≤ 16,000 - -30 ≤
nums[i]≤ 30 - Every product of a part of
numsfits in a 32-bit integer.
Example 1
Input
nums = [1,2,3,4]
Output
[24,12,8,6]
Why:
For example, 24 = 2 × 3 × 4 leaves out the 1.
Example 2
Input
nums = [-1,1,0,-3,3]
Output
[0,0,9,0,0]
Why:
Only the zero's own position escapes the zero: -1 × 1 × -3 × 3 = 9.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –