Skip to content

Commit 564f163

Browse files
committed
product of array except self solution
1 parent 3f859c0 commit 564f163

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def productExceptSelf(self, nums):
3+
n = len(nums)
4+
result = [1] * n
5+
6+
left_product = 1
7+
for i in range(n):
8+
result[i] = left_product
9+
left_product *= nums[i]
10+
11+
right_product = 1
12+
for i in range(n - 1, -1, -1):
13+
result[i] *= right_product
14+
right_product *= nums[i]
15+
16+
return result

0 commit comments

Comments
 (0)