-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.py
More file actions
29 lines (23 loc) · 721 Bytes
/
3sum.py
File metadata and controls
29 lines (23 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def threeSum(self, nums: List[int]) -> List[List[int]]:
"""
Hash set approach didnt work :( , not treid)
use sort and 2 pointers
"""
nums.sort()
arr = set()
for i in range(len(nums)):
curr_val = nums[i]
left = i+1
right = len(nums)-1
target_val = 0-curr_val
while left<right:
effec_val = nums[left]+nums[right]
if effec_val==target_val:
arr.add((curr_val,nums[left],nums[right]))
left+=1
right-=1
elif effec_val<target_val:
left+=1
else:
right-=1
return list(arr)