Leetcode Practice
LC 238. Product of Array Except Self
- Last updated
- Reading time
- 1 min read
The problem
First Solution
function productExceptSelf(nums: number[]): number[] {
let arr = []
let currentProduct = 1
for (let i = 0; i < nums.length; i++) {
arr.push(currentProduct)
currentProduct *= nums[i]
}
currentProduct = 1
for (let i = nums.length - 1; i >= 0; i--) {
arr[i] *= currentProduct
currentProduct *= nums[i]
}
return arr
}