-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-3Sum.py
28 lines (26 loc) · 876 Bytes
/
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
class Solution(object):
def threeSum(self, nums):
res = []
n = len(nums)
nums.sort()
for i in range(n - 2):
# skip the duplicates values
if i > 0 and nums[i] == nums[i - 1]:
continue
# two pointers technique
l = i + 1
r = n - 1
while l < r:
threeSum = nums[l] + nums[i] + nums[r]
if threeSum < 0:
l += 1
elif threeSum > 0:
r -= 1
else:
res.append([nums[l], nums[i], nums[r]])
l += 1
r -= 1
# check if current nums[l] is equal to the prev element nums[l-1]
while l < r and nums[l] == nums[l - 1]:
l += 1
return res