-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path238-product-of-array-except-self.py
36 lines (25 loc) · 1.15 KB
/
238-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
30
31
32
33
34
35
36
#238. Product of Array Except Self Problem Description:
#Given an integer array nums, return an array anwser such that anwser[i]
#is equal to the product of all the elements of nums except nums[i]
class solution(object):
def productExceptSelf(self, nums):
#create an integer array with all ones that is the size of the input array
res = [1] * (len(nums))
#initialize the prefix value as 1
prefix = 1
#iterate through the input array nums
for i in range(len(nums)):
#set all values in the results array as the prefix
res[i] = prefix
#update the value of the prefix for the next iteration
prefix *= nums[i]
#initialize the postifix value as 1
postfix = 1
#iterate through the array backwards
for i in range(len(nums) - 1, -1, -1):
#multiply the values in the results array by the postfix value
res[i] *= postfix
#update the postfix value for the next iteration
postfix *= nums[i]
#return the results array as the anwser
return res