-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15. 3Sum.py
37 lines (27 loc) · 1.07 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
30
31
32
33
34
35
36
37
# O(n^2) Solution
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
nums.sort()
for i in range (len(nums)):
if i != 0 and nums[i] == nums[i-1]:
continue
target = 0 - nums[i]
low, high = i+1,len(nums)-1
while low < high:
if nums[low] + nums[high] == target:
result.append([nums[i], nums[low], nums[high]])
while low < high and nums[low] == nums[low+1]:
low = low + 1
while low < high and nums[high] == nums[high - 1]:
high = high - 1
low, high = low +1, high-1
elif (nums[low] + nums[high]) < target:
low = low + 1
else:
high = high -1
return result