@Hadev
하댑의 개발 기록
@Hadev
전체 방문자
오늘
어제
  • All categories (65)
    • 📒 Tech Note (56)
      • Flutter (0)
      • Unity C# (1)
      • 웹 프로그래밍 (12)
      • CS 기본기 뚝딱 (3)
      • 알고리즘 & 자료구조 (10)
      • DB (6)
      • Cloud (10)
      • DevOps (14)
    • 🔖 Story (9)
      • 💻 개발 언저리 공부 (4)
      • ⛵️ 취미 & 팁 (5)
      • 💸 재테크 (0)

인기 글

티스토리

hELLO · Designed By 정상우.
@Hadev
🚀 Hadev Tech Blog
ABOUT
TAG
GUESTBOOK
[LeetCode] 238. Product of Array Except Self
📒 Tech Note/알고리즘 & 자료구조

[LeetCode] 238. Product of Array Except Self

2022. 8. 24. 23:23

*알고리즘 스터디에 참여하면서 Blind 75 LeetCode Questions 목록에 있는 문제를 풀이합니다. 

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

 

Product of Array Except Self - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

배열에서 현재 요소를 뺀 나머지 요소들을 곱한 값을 그 자리에 넣어서 Return 해야한다.
1. 입력한 리스트와 같은 크기의 리스트를 출력해야하고, 반복문이 최소 1번 나온다.
2. 나누기를 사용하면 안되고, 복잡도는 O(n)으로 제한하고 있기 때문에 이중 반복문을 이용해서 풀어서는 안된다.

풀이:

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        res = []
        p = 1
        # 왼쪽에서부터 차례대로 곱셈
        for i in range(len(nums)):
            res.append(p)
            p = p * nums[i]
            
        p = 1
        # 왼쪽 곱셈 결과에 오른쪽값을 차례대로 곱셈
        for i in range(len(nums) - 1, - 1, -1):
            res[i] = res[i] * p
            p = p * nums[i]
            
        return res
저작자표시 비영리 변경금지 (새창열림)

'📒 Tech Note > 알고리즘 & 자료구조' 카테고리의 다른 글

[번역] 14가지 패턴으로 코딩 인터뷰 완전 정복하기  (0) 2022.09.08
[LeetCode] 53. Maximum Subarray  (0) 2022.09.03
[LeetCode] 217. Contains Duplicate  (0) 2022.08.24
[LeetCode] 121. Best Time to Buy and Sell Stock  (0) 2022.08.20
[LeetCode] 1. Two Sum  (0) 2022.08.20
    '📒 Tech Note/알고리즘 & 자료구조' 카테고리의 다른 글
    • [번역] 14가지 패턴으로 코딩 인터뷰 완전 정복하기
    • [LeetCode] 53. Maximum Subarray
    • [LeetCode] 217. Contains Duplicate
    • [LeetCode] 121. Best Time to Buy and Sell Stock
    @Hadev
    @Hadev

    티스토리툴바