#238

Product of Array Except Self

pupil · 580 · lc medium +29 · verified · 68.6% accepted · 25,750 likes · top 76%

play →

Description

Given an integer array nums, construct and return an array answer where answer[i] is the product of all elements of nums except nums[i].

Every prefix and suffix product is guaranteed to fit in a 32-bit integer.

Your solution must run in O(n) time and must not use the division operator.

Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]

Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]

Code

1
2
3