-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path15.py
More file actions
32 lines (30 loc) · 900 Bytes
/
Copy path15.py
File metadata and controls
32 lines (30 loc) · 900 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
30
31
class Solution:
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
nums.sort()
print(nums)
i = 0
while i < len(nums):
target = -nums[i]
x, y = i + 1, len(nums) - 1
while x < y:
if nums[x] + nums[y] < target:
x += 1
elif nums[x] + nums[y] > target:
y -= 1
else:
X = nums[x]
Y = nums[y]
res.append([nums[i], X, Y])
while x < y and nums[x] == X:
x += 1
while x < y and nums[y] == Y:
y -= 1
while i + 1 < len(nums) and nums[i + 1] == nums[i]:
i += 1
i += 1
return res