-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path0015-3sum.py
34 lines (26 loc) · 922 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
29
30
31
32
33
34
class Solution:
def threeSum(self, nums):
final_list = []
nums.sort()
for j in range(len(nums)):
if((j>0) and (nums[j]==nums[j-1])):
continue
value = -nums[j]
first = j + 1
last = len(nums) - 1
while first<last:
s = nums[first] + nums[last]
if s > value:
last -= 1
elif s < value:
first += 1
else:
x = nums[j]
y = nums[first]
z = nums[last]
final_list.append([x,y,z])
while((first<last) and (nums[first]==y)):
first = first + 1
while((first<last) and (nums[last]==z)):
last = last - 1
return final_list