Published:
Updated:

  • Reference
  • ์ด ๋กœ์ง์€ ๋ถ„๋ช… ๋‚˜์ค‘์— ์–ด๋””์„œ ๋˜ ์“ฐ์ผ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์™ผ์ชฝ ํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ ํ•ด์„œ ๊ณ„์‚ฐํ•œ๋‹ค๋Š” ์  ๊ธฐ์–ตํ•ด ๋‘์ž.


Solution

from typing import List


class Solution:
    # ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ ์“ฐ์ง€ ๋ง๊ณ , O(N)์œผ๋กœ
    # ๋‚˜๋ˆ„๊ธฐ ์“ธ ์ˆ˜ ์žˆ์œผ๋ฉด sum(nums)ํ•œ ๋‹ค์Œ์— ํ•˜๋‚˜์”ฉ ๋‚˜๋ˆ ์ฃผ๋ฉด ๋์ด๊ธด ํ•จ
    # ๊ฒฐ๊ตญ ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชป ํ•˜๋‹ˆ๊นŒ ์ž์‹ ์„ ์ œ์™ธํ•œ ๊ณฑ์…ˆ๋“ค์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ์—†์Œ
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        answer = []

        # ์™ผ์ชฝ๋ถ€ํ„ฐ ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ answer ๋ฐฐ์—ด์— ์ €์žฅ
        times = 1
        for num in nums:
            answer.append(times)
            times *= num

        times = 1
        # ์™ผ์ชฝ๋ถ€ํ„ฐ ๊ณฑํ•œ ๊ฒฐ๊ณผ์— ์˜ค๋ฅธ์ชฝ ๊ฐ’๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๊ณฑํ•ด์„œ ์ €์žฅํ•˜์ž
        for i in range(len(nums) - 1, -1, -1):
            answer[i] *= times
            times *= nums[i]

        return answer

Leave a comment