Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 596 Bytes

238.-product-of-array-except-self.md

File metadata and controls

25 lines (21 loc) · 596 Bytes

238. Product of Array Except Self

{% tabs %} {% tab title="python" %}

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        
        product = 1
        ans = [1]
        ## 数组操作, 前缀积
        for i in range(len(nums) - 1):
            ans.append(product * nums[i])
            product = ans[-1]
        ## get the left product
        product = 1
        for i in range(len(nums) - 1, -1, -1):
            ans[i] = ans[i] * product
            product = nums[i] * product
            
        return ans

{% endtab %} {% endtabs %}