File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments