python-algorithm

leetcode 238. Product of Array Except Self

무적김두칠 2023. 2. 10. 00:17

https://leetcode.com/problems/product-of-array-except-self/description/

 

Product of Array Except Self - LeetCode

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.

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        out = []
        p = 1
 
        for i in range(0len(nums)):
            out.append(p)
            p *= nums[i]
        
        p = 1
        for i in range(len(nums)-10 -1-1):
            out[i]*=p
            p*=nums[i]
        
        return out
cs
반응형