PS/리트코드
[리트코드] 238. Product of Array Except Self
Mingi Kim
2024. 7. 10. 10:57
문제 링크
https://leetcode.com/problems/product-of-array-except-self/description/
느낀 점
아이디어를 생각해내는 것이 상당히 어려운 문제였다.. 핵심은 자기 자신의 왼쪽과 오른쪽의 곱을 모두 곱하는 것이었는데, 그 값들을 얻어내는 과정은 때려맞춘 감이 없지않아 있는 문제..
from typing import List
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
answer = []
p = 1
for i in range(len(nums)):
answer.append(p)
p *= nums[i]
p = 1
for j in range(len(nums)-1, -1, -1):
answer[j] *= p
p *= nums[j]
return answer
print(Solution().productExceptSelf([1,2,3,4]))