-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.py
More file actions
29 lines (24 loc) · 789 Bytes
/
Copy path3sum.py
File metadata and controls
29 lines (24 loc) · 789 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
27
28
29
class Solution:
def threeSum(self, nums: [int]) -> [[int]]:
nums.sort()
maxl = len(nums)
ans = []
for z in range(maxl-2):
if z > 0 and nums[z] == nums[z-1]:
continue
l, r = z+1, maxl-1
while l < r:
tsum = nums[l] + nums[r] + nums[z]
if tsum < 0:
l += 1
elif tsum > 0:
r -= 1
else:
ans.append([nums[l], nums[r], nums[z]])
while l < r and nums[l] == nums[l+1]:
l += 1
while l < r and nums[r] == nums[r-1]:
r -= 1
l += 1
r -= 1
return ans