---
# System prepended metadata

title: 238. Product of Array Except Self

---

# 238. Product of Array Except Self

壓縮前：
```python=
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        accum = 1
        res = [1 for _ in range(len(nums))]
        
        for i in range(1, len(nums)):
            accum *= nums[i-1]
            res[i] *= accum
        
        accum = 1
        for i in range(len(nums)-2, -1, -1):
            accum *= nums[i+1]
            res[i] *= accum
            
        return res
```
壓縮後：

```python=
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        accum1, accum2 = 1, 1
        res = [1 for _ in range(len(nums))]
        
        for i in range(1, len(nums)):
            accum1 *= nums[i-1]
            res[i] *= accum1
            accum2 *= nums[len(nums) - i]
            res[len(nums) - i - 1] *= accum2
        
        return res
```
