Product of Array Except Self
LeetCode 238
class Solution:
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
"""
Input: [1,2,3,4]
Output: [24,12,8,6]
24 = 2 * 3 * 4
12 = 1 * 3 * 4
此題不能用除法
[a, b, c, d]
| /| / |
[1 1*a 1*a*b 1*a*b*c ]
再倒著回來做一遍即可得到答案
"""
p = 1
res = []
for i in range(0, len(nums)):
output.append(p)
p = p * nums[i]
p = 1
for i in range(n-1,-1,-1):
output[i] = output[i] * p
p = p * nums[i]
return res
Last updated