Skip to content

Commit bf1ed65

Browse files
authored
Added Python solution for 238.Product of Array Except Self (#138)
1 parent df65d35 commit bf1ed65

File tree

1 file changed

+15
-0
lines changed
  • Algorithms/Medium/238_ProductOfArrayExceptSelf

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
prefix_prod = 1
4+
postfix_prod = 1
5+
res = []
6+
7+
for i in range(len(nums)):
8+
res.append(prefix_prod)
9+
prefix_prod *= nums[i]
10+
11+
for i in range(len(nums) - 1, -1, -1):
12+
res[i] = res[i] * postfix_prod
13+
postfix_prod *= nums[i]
14+
15+
return res

0 commit comments

Comments
 (0)