-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-3Sum.py
29 lines (26 loc) · 1.06 KB
/
15-3Sum.py
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
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) == 3:
if sum(nums) == 0:
return [nums]
return []
indices = []
nums.sort()
for i, n1 in enumerate(nums):
if i > len(nums) - 3:
break
if i > 0 and n1 == nums[i-1]:
continue
low, high = i+1, len(nums) - 1
while low < high:
sum_ = n1 + nums[low] + nums[high]
if sum_ < 0:
low += 1
elif sum_ > 0:
high -= 1
else:
indices.append([n1, nums[low], nums[high]])
low += 1
while low < len(nums) and nums[low] == nums[low-1]:
low += 1
return indices