-
Notifications
You must be signed in to change notification settings - Fork 0
/
#0238 Product of Array Except Self.py
29 lines (23 loc) · 1.14 KB
/
#0238 Product of Array Except Self.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import List
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
"""
Returns an array such that each element at index 'i' is the product of all
elements in the original array except for the element at index 'i'.
Args:
nums (List[int]): A list of integers.
Returns:
List[int]: A list where each element is the product of all other elements in 'nums'.
"""
results = [1] * len(nums) # Initialize the results array with 1s
# Calculate the prefix products and store them in results
prefix = 1
for i in range(len(nums)):
results[i] = prefix # Store the current prefix product
prefix *= nums[i] # Update prefix with the current element
# Calculate the postfix products and multiply them with the current value in results
postfix = 1
for i in range(len(nums) - 1, -1, -1):
results[i] *= postfix # Multiply the current result with the postfix product
postfix *= nums[i] # Update postfix with the current element
return results