forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProd_Array_Leetcode_238
More file actions
34 lines (27 loc) · 818 Bytes
/
Prod_Array_Leetcode_238
File metadata and controls
34 lines (27 loc) · 818 Bytes
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
def productarr(nums):
left_prod = []
for i in range(0, len(nums)):
if i == 0:
left_prod.append(nums[i])
else:
left_prod.append(left_prod[i - 1] * nums[i])
right_prod = []
j = 0
for i in range(len(nums) - 1, -1, -1):
if i == len(nums) - 1:
right_prod.append(nums[i])
else:
right_prod.append(right_prod[j - 1] * nums[i])
j += 1
right_product=right_prod[::-1]
result = []
for i in range(0, len(nums)):
if i == 0:
result.append(1 * right_product[i + 1])
elif i == len(nums) - 1:
result.append(left_prod[i - 1] * 1)
else:
result.append(left_prod[i - 1] * right_product[i + 1])
return result
num=[1,2,3,4]
print(productarr(num))