forked from avani112/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSumZero.py
More file actions
26 lines (26 loc) · 906 Bytes
/
threeSumZero.py
File metadata and controls
26 lines (26 loc) · 906 Bytes
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
class Solution:
def threeSumZero(self, nums: List[int]) -> List[List[int]]:
rt = []
if(len(nums) < 3):
return []
nums.sort()
for i in range(len(nums)-1):
if not(i>0 and nums[i] == nums[i-1]):
tar = - nums[i]
hi = len(nums)-1
l = i+1
while l < hi:
tg = nums[hi] + nums[l]
if tg < tar:
l+=1
elif tg > tar:
hi-=1
else:
rt.append([nums[i] , nums[l] ,nums[hi]])
hi -=1
l+=1
while hi >l and nums[hi+1] == nums[hi]:
hi -=1
while hi >l and nums[l-1] == nums[l]:
l +=1
return rt