본문 바로가기
python-algorithm

leetcode 238. Product of Array Except Self

by 무적김두칠 2023. 2. 10.

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
반응형

'python-algorithm' 카테고리의 다른 글

leetcode 121. Best Time to Buy and Sell Stock  (0) 2023.02.11
leetcode 2149. Rearrange Array Elements by Sign  (0) 2023.02.10
leetcode 15. 3Sum  (0) 2023.02.09
leetcode 42. Trapping Rain Water  (0) 2023.02.09
백준 27389 Metronome  (0) 2023.02.09

댓글