Skip to content

Commit db8e2de

Browse files
committed
product-of-array-except-self
1 parent 2ed6480 commit db8e2de

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
"""
4+
์ฒ˜์Œ์— ๋ชจ๋‘ ๊ณฑํ•œ๊ฑธ ๋‚˜๋ˆˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์œผ๋‚˜, 0์„ ๋‚˜๋ˆ„๊ธฐ ํž˜๋“ค๊ณ  ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋‹ต์ด ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ•จ.
5+
๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ž์‹ ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์˜ ๊ณฑ์„ ๊ณ„์‚ฐ.
6+
์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ๊ณฑ์„ ๊ณ„์‚ฐํ•˜์—ฌ answer[i]์— ์ €์žฅํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ๊ณฑ์„ ๊ณ„์‚ฐํ•˜์—ฌ answer[i]์— ๊ณฑํ•จ.
7+
์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๋ฐฐ์—ด์„ ๋‘ ๋ฒˆ ์ˆœํšŒํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐ.
8+
"""
9+
n = len(nums)
10+
answer = [1] * n
11+
12+
left = 1
13+
for i in range(n):
14+
answer[i] = left
15+
left *= nums[i]
16+
right = 1
17+
for i in range(n-1, -1, -1):
18+
answer[i] *= right
19+
right *= nums[i]
20+
21+
return answer

0 commit comments

Comments
ย (0)