-
Notifications
You must be signed in to change notification settings - Fork 0
/
15. 3 sum.py
34 lines (32 loc) · 1.09 KB
/
15. 3 sum.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(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
i = 0
j = len(nums) -1
nums.sort()
result = []
if nums[0]>0:
return []
for i in range(0,len(nums)-2,1):
for j in range(len(nums)-1,2,-1):
if i + j < 0:
tail = j -1
while nums[i] + nums[j] + nums[tail] != 0 and tail > i:
tail -=1
if nums[i] + nums[j] + nums[tail] == 0:
result.append([nums[i], nums[j], nums[tail]])
elif i +j >= 0:
tail = i+1
while nums[i] + nums[j] + nums[tail] != 0 and tail < i:
tail +=1
if nums[i] + nums[j] + nums[tail] == 0:
result.append([nums[i],nums[j],nums[tail]])
else:
pass
print(result)
nums = [-1, 0, 1, 2, -1, -4]
so = Solution()
so.threeSum(nums)