-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeSum.py
30 lines (29 loc) · 938 Bytes
/
threeSum.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
class Solution:
# @param {integer[]} nums
# @return {integer[][]}
def threeSum(self, nums):
nums.sort()
res = []
n = len(nums)
i = 0
while(i<n):
t2 = 0 - nums[i]
front = i + 1
back = n -1
while(front < back):
sum = nums[front] + nums[back]
if sum < t2:
front += 1
elif sum > t2:
back -= 1
else:
tripple = [nums[i],nums[front], nums[back]]
while(front < back and nums[front]== tripple[1]):
front += 1
while(front <back and nums[back] == tripple[2]):
back -= 1
res.append(tripple)
while(i+1 < n and nums[i+1] == nums[i]):
i += 1
i += 1
return res