-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015_3sum.py
43 lines (38 loc) · 1.29 KB
/
015_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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
results = []
nums.sort()
for i in range(len(nums)):
if nums[i] == nums[i - 1] and i > 0:
continue
left = i + 1
right = len(nums) - 1
while left < right:
sum = nums[i] + nums[left] + nums[right]
if sum > 0:
right -= 1
elif sum < 0:
left += 1
else:
results.append([nums[i], nums[left], nums[right]])
left += 1
right -= 1
try:
while nums[left] == nums[left - 1]:
left += 1
except IndexError:
pass
try:
if nums[right] == nums[right + 1]:
right -= 1
except IndexError:
pass
return results
print(Solution().threeSum([-1, 0, 1, 2, -1, -4]))
print(Solution().threeSum([0, 0, 0]))
print(Solution().threeSum([-2,0,3,-1,4,0,3,4,1,1,1,-3,-5,4,0]))
print(Solution().threeSum([-2, 0, 0, 2, 2]))