-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathThree_Sum.py
More file actions
40 lines (36 loc) · 1.1 KB
/
Three_Sum.py
File metadata and controls
40 lines (36 loc) · 1.1 KB
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
38
39
40
"""
@author - Anirudh Sharma
"""
from typing import List
def threeSum(nums: List[int]) -> List[List[int]]:
# Sort the given array
nums.sort()
# Length of the array
n = len(nums)
# Resultant list
triplets = list()
# Loop for each character in the array
for i in range(0, n):
# Avoid duplicates due to i
if i > 0 and nums[i] == nums[i - 1]:
continue
# Left and right pointers
j = i + 1
k = n - 1
# Loop for remaining pairs
while j < k:
if nums[i] + nums[j] + nums[k] == 0:
triplets.append([nums[i], nums[j], nums[k]])
j += 1
# Avoid duplicates for j
while j < k and nums[j] == nums[j - 1]:
j += 1
elif nums[i] + nums[j] + nums[k] < 0:
j += 1
else:
k -= 1
return triplets
if __name__ == '__main__':
print(threeSum([-1, 0, 1, 2, -1, -4]))
print(threeSum([]))
print(threeSum([-1, -2, 3, -4, -5, 9, -10, -11, 21, -22, -23, 45, -46, -47, 93, -94, -95, 189]))