-
Notifications
You must be signed in to change notification settings - Fork 0
/
3sum.py
32 lines (32 loc) · 1.01 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
class Solution:
#given a list of integers
#return a list of lists of integers
def threeSum(self, num):
num.sort()
res = []
i = 0
while i < len(num) - 2:
j = i + 1
k = len(num) - 1
while j < k:
tmp = num[i] + num[j] + num[k]
if tmp < 0:
j += 1
while j < k and num[j] == num[j-1]:
j += 1
elif tmp > 0:
k -= 1
while j < k and num[k] == num[k+1]:
k -= 1
else:
res.append([num[i], num[j], num[k]])
j += 1
k -= 1
while j < k and num[j] == num[j-1]:
j += 1
while j < k and num[k] == num[k+1]:
k -= 1
i += 1
while i < len(num) - 2 and num[i] == num[i-1]:
i += 1
return res