# 238 product of array except self

### 238. Product of Array Except Self

Medium

class Solution(object):
def productExceptSelf(self,nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
lst = []
for i in range(len(nums)):
lst.append(self.productWithoutI(nums,i))
return lst

def productWithoutI(self,nums,i):
product = 1
for j in range(len(nums)):
if j != i:
product *= nums[j]
return product


output[i] =  { i 前面的数的乘积}  X  { i 后面的数的乘积}

class Solution(object):
def productExceptSelf(self,nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if nums == [] : return []
lft = [1]
rgt = [1]
product = 1
for i in range(1,len(nums)):
product *= nums[i-1]
lft.append(product)
product = 1
for i in reversed(range(1,len(nums))):
product *= nums[i]
rgt.append(product)
rgt.reverse()
result = []
for i in range(len(nums)):
result.append(lft[i]*rgt[i])
return result



class Solution(object):
def productExceptSelf(self,nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if nums == [] : return []
size = len(nums)
output = [1] * size
left = 1
for x in range(size-1):
left *= nums[x]
output[x+1] *= left
right = 1
for x in range(size - 1, 0, -1):
right *= nums[x]
output[x-1] *= right
return output


apachecn/AiLearning