-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path18_three_sum.py
More file actions
37 lines (28 loc) · 774 Bytes
/
18_three_sum.py
File metadata and controls
37 lines (28 loc) · 774 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
30
31
32
33
34
35
36
37
def threeSum(nums: list[int]) -> list[list[int]]:
res = []
nums.sort()
for i, a in enumerate(nums):
if a > 0:
break
if i > 0 and a == nums[i - 1]:
continue
l, r = i + 1, len(nums) - 1
while l < r:
threeSum = a + nums[l] + nums[r]
if threeSum > 0:
r -= 1
elif threeSum < 0:
l += 1
else:
res.append([a, nums[l], nums[r]])
l += 1
while nums[l] == nums[l - 1] and l < r:
l += 1
return res
if __name__ == "__main__":
X = [-1,0,1,2,-1,-4]
Y = [0,1,1]
Z = [0,0,0]
print(threeSum(X))
print(threeSum(Y))
print(threeSum(Z))