-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.py
More file actions
29 lines (29 loc) · 928 Bytes
/
threeSum.py
File metadata and controls
29 lines (29 loc) · 928 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
import bisect
class Solution:
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
res = []
t = bisect.bisect_right(nums, 0)
for i in range(t):
if i == 0 or nums[i] >nums[i-1]:
l = i+1
r = len(nums) -1
while l < r:
A = nums[i] + nums[l] + nums[r]
if A == 0:
res.append([nums[i], nums[l], nums[r]])
l += 1
r -= 1
while l < r and nums[l] == nums[l-1]:
l += 1
while r > l and nums[r+1] == nums[r]:
r -= 1
elif A >0:
r -= 1
elif A < 0:
l += 1
return res