forked from super30admin/Array-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp26_medium_arrayproductexceptself.py
More file actions
53 lines (34 loc) · 1.26 KB
/
p26_medium_arrayproductexceptself.py
File metadata and controls
53 lines (34 loc) · 1.26 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Leetcode: https://leetcode.com/problems/product-of-array-except-self/
Given an array nums of n integers where n > 1,
return an array output such that output[i] is equal to the product of all the
elements of nums except nums[i].
Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in O(n).
:return
"""
class Solution:
def productExceptSelf(self, nums):
leftrunningproductList=[None]*len(nums)
# print(len(leftrunningproductList))
rightrunningproductList=[None]*len(nums)
##for calculating left
rp=1
leftrunningproductList[0]=1
for i in range(1, len(nums)):
rp=rp*nums[i-1]
leftrunningproductList[i]=rp
print(leftrunningproductList)
# return leftrunningproductList
rp = 1
rightrunningproductList[len(nums)-1]=1
for i in range(len(nums)-2,-1,-1):
rp=rp*nums[i+1]
rightrunningproductList[i]=rp
print(rightrunningproductList)
for i in range(len(nums)):
leftrunningproductList[i]=leftrunningproductList[i]* rightrunningproductList[i]
return leftrunningproductList
nums = [1, 2, 3, 4]
print(nums)
solve=Solution()
print(solve.productExceptSelf(nums))