-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_three_sum_1.py
33 lines (31 loc) · 1.01 KB
/
15_three_sum_1.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
30
31
32
33
from typing import List
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
l = len(nums)
ret = []
for k in range(l-2):
if nums[k] > 0:
break
if k > 0 and nums[k] == nums[k-1]:
continue
i, j = k+1, l-1
while i < j:
s = nums[k] + nums[i] + nums[j]
if s > 0:
j -= 1
while j > i and nums[j] == nums[j+1]:
j -= 1
elif s < 0:
i += 1
while i < j and nums[i] == nums[i-1]:
i += 1
else:
ret.append([nums[k], nums[i], nums[j]])
i += 1
j -= 1
while i < j and nums[i] == nums[i-1]:
i += 1
while j > i and nums[j] == nums[j+1]:
j -= 1
return ret