# [leetcode] 238. Product of Array Except Self _ Algorithm Problem Solve for python

## 1. Problem

238. Product of Array Except Self

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]


Example 2:

Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]


Constraints:

• 2 <= nums.length <= 10^5
• -30 <= nums[i] <= 30
• The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

## 2. Solution

I solved this problem like this.

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
if nums.count(0) >= 2:
return [0] * len(nums)
elif nums.count(0) == 1:
total = reduce((lambda x, y: x * y), [x for x in nums if x != 0])
return [0 if num != 0 else total for num in nums]
else:
total = reduce((lambda x, y: x * y), nums)
return [total//num for num in nums]
return []