Skip to content

Commit 24dd38c

Browse files
committed
238. Product of Array Except Self Solution
1 parent e8b19f6 commit 24dd38c

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+
# ๊ฐ ์ธ๋ฑ์Šค ๊ธฐ์ค€ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฐ’๋“ค์˜ ๊ณฑ์„ l_arr ์— ์ €์žฅํ•œ๋‹ค.
2+
# ๊ฐ ์ธ๋ฑ์Šค ๊ธฐ์ค€ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฐ’๋“ค์˜ ๊ณฑ์„ r_arr ์— ์ €์žฅํ•œ๋‹ค.
3+
# ๋งˆ์ง€๋ง‰์— ๊ฐ™์€ ์ธ๋ฑ์Šค์˜ l_arr ๊ฐ’๊ณผ r_arr ๊ฐ’์„ ๊ณฑํ•ด์„œ
4+
# => ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์›์†Œ์˜ ๊ณฑ์„ ๊ตฌํ•œ๋‹ค.
5+
class Solution:
6+
def productExceptSelf(self, nums: List[int]) -> List[int]:
7+
n = len(nums)
8+
l_arr = [0] * n # ์™ผ์ชฝ ๋ˆ„์  ๊ณฑ
9+
r_arr = [0] * n # ์˜ค๋ฅธ์ชฝ ๋ˆ„์  ๊ณฑ
10+
11+
l_mult = 1
12+
r_mult = 1
13+
14+
for i in range(n):
15+
j = -i -1
16+
l_arr[i] = l_mult
17+
r_arr[j] = r_mult
18+
l_mult *= nums[i]
19+
r_mult *= nums[j]
20+
21+
return [ l*r for l, r in zip(l_arr, r_arr)]

0 commit comments

Comments
ย (0)