-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy path238.py
More file actions
30 lines (25 loc) · 701 Bytes
/
238.py
File metadata and controls
30 lines (25 loc) · 701 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
'''
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]
1 1 2 6
12 8 6
'''
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:
return []
dp = [1]*len(nums)
for index in range(1,len(nums)):
dp[index] = dp[index-1]*nums[index-1]
print dp
right = 1
for index in range(len(nums)-1, -1, -1):
dp[index] *= right
right *= nums[index]
return dp