-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.py
More file actions
20 lines (20 loc) · 681 Bytes
/
Copy path3sum.py
File metadata and controls
20 lines (20 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
ans = set()
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, len(nums) - 1
target = -nums[i]
while left < right:
s = nums[left] + nums[right]
if s == target:
ans.add((nums[i], nums[left], nums[right]))
left += 1
right -= 1
elif s < target:
left += 1
else:
right -= 1
return list(ans)