File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
3+ # ans = set()
4+ # for i in range(len(n)):
5+ # seen = {}
6+ # for j in range(i+1, len(n)):
7+ # complement = -(nums[i] + nums[j])
8+ # if complement in seen:
9+ # ans.add(tuple(sorted([nums[i], nums[j], complement])))
10+ # seen[nums[j]] = j
11+ # return [list(triplet) for triplet in ans]
12+ # hash + 이중 loop -> O(n^2)
13+
14+
15+ ## 투포인터 활용
16+ # sort + loop -> O(nlogn)
17+ ans_set = set ()
18+ nums .sort ()
19+ for i in range (len (nums )):
20+ l = i + 1
21+ r = len (nums ) - 1
22+ while l < r :
23+ total = nums [i ] + nums [l ] + nums [r ]
24+ if total < 0 :
25+ l += 1
26+ elif total > 0 :
27+ r -= 1
28+ else :
29+ # 중복제거
30+ ans_set .add ((nums [i ], nums [l ], nums [r ]))
31+ l += 1
32+ r -= 1
33+ return list (ans_set )
34+
35+
You can’t perform that action at this time.
0 commit comments