File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ 1. 3 way for๋ฌธ์ผ๋ก ๋์๊ฐ๋ฉด์ 0์ธ ํฉ์ ์ฐพ๋ ๋ฐฉ๋ฒ
4+ - O(n^3)์ผ๋ก ์๊ฐ์ด๊ณผ
5+ 2. ํฌํฌ์ธํฐ
6+ - ์ ๋ ฌ ํ ํฌํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ์ค๋ณต ์ ๊ฑฐ์ ์ต์ ํ๋ฅผ ๋์์ ์ํ
7+ - O(n^2)
8+ ๊ณต๊ฐ ๋ณต์ก๋๋ ๋๋ค O(1)
9+ """
10+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
11+ result = []
12+ nums .sort ()
13+
14+ for i in range (len (nums )):
15+ if i > 0 and nums [i ] == nums [i - 1 ]:
16+ continue
17+
18+ left , right = i + 1 , len (nums ) - 1
19+ while left < right :
20+ total = nums [i ] + nums [left ] + nums [right ]
21+
22+ if total == 0 :
23+ result .append ([nums [i ], nums [left ], nums [right ]])
24+ left += 1
25+ right -= 1
26+
27+ while left < right and nums [left ] == nums [left - 1 ]:
28+ left += 1
29+ while left < right and nums [right ] == nums [right + 1 ]:
30+ right -= 1
31+
32+ elif total < 0 :
33+ left += 1
34+ else :
35+ right -= 1
36+
37+ return result
You canโt perform that action at this time.
0 commit comments