-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path0015_3Sum.py
28 lines (25 loc) · 963 Bytes
/
0015_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):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
nums.sort()
for first_index in xrange(len(nums)):
if first_index - 1 >= 0 and nums[first_index] == nums[first_index-1]:
continue
start, end = first_index + 1, len(nums)-1
while start < end :
tmp_sum = nums[first_index] + nums[start] + nums[end]
if tmp_sum == 0:
result.append([nums[first_index], nums[start], nums[end]])
start += 1
end -= 1
while start < end and nums[start] == nums[start-1]:
start+=1
elif tmp_sum < 0:
start += 1
else:
end -= 1
return result