File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ class Solution :
4+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
5+ nums .sort ()
6+ n = len (nums )
7+ res : List [List [int ]] = []
8+
9+ for i in range (n - 2 ):
10+ # i 중복 스킵
11+ if i > 0 and nums [i ] == nums [i - 1 ]:
12+ continue
13+
14+ # nums[i] 이후는 전부 양수 → 더 이상 0 못 만듦
15+ if nums [i ] > 0 :
16+ break
17+
18+ left , right = i + 1 , n - 1
19+
20+ while left < right :
21+ total = nums [i ] + nums [left ] + nums [right ]
22+
23+ if total == 0 :
24+ res .append ([nums [i ], nums [left ], nums [right ]])
25+
26+ left += 1
27+ right -= 1
28+
29+ # left 중복 스킵
30+ while left < right and nums [left ] == nums [left - 1 ]:
31+ left += 1
32+
33+ # right 중복 스킵
34+ while left < right and nums [right ] == nums [right + 1 ]:
35+ right -= 1
36+
37+ elif total < 0 :
38+ left += 1
39+ else :
40+ right -= 1
41+
42+ return res
You can’t perform that action at this time.
0 commit comments