238. Product of Array Except Self
https://leetcode.com/problems/product-of-array-except-self/description/
Input: [1,2,3,4] Output: [24,12,8,6]
Thoughts
Code
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
N, r = len(nums), 1
res = [1] * N
for i in range(1, N):
res[i] = res[i - 1] * nums[i - 1]
for i in reversed(range(N)):
res[i] *= r
r *= nums[i]
return res
Analysis
Last updated