forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.py
More file actions
35 lines (33 loc) · 1.21 KB
/
3sum.py
File metadata and controls
35 lines (33 loc) · 1.21 KB
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
# -*- coding: utf-8 -*-
class Solution:
"""
@param numbersbers : Give an array numbersbers of n integer
@return : Find all unique triplets in the array which gives the sum of zero.
"""
def threeSum(self, numbers):
# write your code here
self.ret = []
numbers.sort() # 排序后搜索,重点是去重!
for i in xrange(len(numbers)):
if i > 0: # 第一个元素已经用过必需跳过
if numbers[i] == numbers[i - 1]:
continue
self.twoSum(numbers, -numbers[i], i + 1, len(numbers) - 1)
return self.ret
def twoSum(self, numbers, target, start, end):
while start < end:
x, y = numbers[start], numbers[end]
if (x + y) == target:
self.ret.append([-target, x, y])
start += 1
end -= 1
# 跳过重复元素
while (start < end) and (numbers[start] == x):
start += 1
while (start < end) and (numbers[end] == y):
end -= 1
elif (x + y) < target:
start += 1
else:
end -= 1
return ret