-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.py
45 lines (32 loc) · 1.13 KB
/
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
44
45
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
ll = len(nums)
res = []
for idx in xrange(ll - 2):
if idx == 0 or nums[idx] > nums[idx - 1]:
start = idx + 1
end = ll - 1
neg = -nums[idx]
while start < end:
cur_sum = nums[start] + nums[end]
if cur_sum == neg:
cur = [nums[idx], nums[start], nums[end]]
res.append(cur)
start += 1
end -= 1
while start < end and nums[end] == nums[end + 1]:
end -= 1
while start < end and nums[start] == nums[start - 1]:
start += 1
elif cur_sum < neg:
start += 1
else:
end -= 1
return res
input = [-2,0,1,1,2] # [[-2,0,2],[-2,1,1]]
print Solution().threeSum(input)