Skip to content

Commit 1691310

Browse files
committed
product-of-array-except-selt solution
1 parent f34b332 commit 1691310

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
n = len(nums)
4+
answer = [1] * n
5+
6+
# 1. 왼쪽 곱 저장
7+
left_product = 1
8+
for i in range(n):
9+
answer[i] = left_product
10+
left_product *= nums[i]
11+
12+
# 2. 오른쪽 곱을 곱해주기
13+
right_product = 1
14+
for i in reversed(range(n)):
15+
answer[i] *= right_product
16+
right_product *= nums[i]
17+
18+
return answer

0 commit comments

Comments
 (0)