LeetCode 238. Product of Array Except Self

개발공부를해보자·2025년 1월 9일

LeetCode

목록 보기
11/95

파이썬 알고리즘 인터뷰 문제 11번(리트코드 238번) Product of Array Except Self
https://leetcode.com/problems/product-of-array-except-self/

나의 풀이

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        result = []

        multiple = 1
        for left in range(len(nums)):
            result.append(multiple)
            multiple *= nums[left]
        
        multiple = 1
        
        for right in range(len(nums) - 1, 0, -1):
            multiple *= nums[right]
            result[right - 1] *= multiple
        
        return result

코드를 짜기 전에 인간인 내가 어떤 식으로 이 문제를 푸는 지 생각해보는 것이 많은 도움이 된다.

profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글