-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sums.py
More file actions
executable file
·95 lines (75 loc) · 3.25 KB
/
Copy path3sums.py
File metadata and controls
executable file
·95 lines (75 loc) · 3.25 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
<<<<<<< HEAD
=======
>>>>>>> eeb5309a91ff43c2bfab146d35663d62a81c2d3e
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
"""
from typing import List
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
"""
Find all triplets in a list of numbers that add up to 0.
Args:
nums: A list of integers.
Returns:
A list of lists of integers, where each list represents a triplet that adds up to 0.
"""
len_nums = len(nums)
if len_nums < 3:
return []
# The code you provided needs to be sorted because
# it uses a technique called two pointers to find the triplets.
# The two pointers are initialized to the first and last elements of the list,
# respectively. Then, the pointers are moved inwards, one at a time.
# At each step, the sum of the two pointers is checked.
# If the sum is equal to the target, then a triplet is found. Otherwise,
# the sum is too small, so the left pointer is moved inwards.
# If the sum is too big, then the right pointer is moved inwards.
nums.sort()
results = []
print(f"nums: {nums}")
for i in range(len(nums)):
# Ignore any duplicate numbers.
if i > 0 and nums[i] == nums[i - 1]:
continue
# calculates the target sum for the current iteration for example,
# if i = 0, target = -nums[0] The target sum is
# the sum of the other two numbers in the triplet that must add up to 0.
# In this case, the target sum is the negative of the current number.
target = -nums[i]
print(f"target: {target}")
# Initialize the pointers for the two other numbers in the triplet.
# j is the left pointer, and k is the right pointer.
# left counter and right counter are opposite direction
j, k = i + 1, len(nums) - 1
print(f"j: {j}, k: {k}")
while j < k:
if nums[j] + nums[k] == target:
# Found a triplet!
results.append([nums[i], nums[j], nums[k]])
j += 1
k -= 1
elif nums[j] + nums[k] < target:
# The sum is too small, so increment the left pointer.
j += 1
else:
# The sum is too big, so decrement the right pointer.
k -= 1
return list(set(tuple(result) for result in results))
<<<<<<< HEAD
=======
>>>>>>> eeb5309a91ff43c2bfab146d35663d62a81c2d3e
solution = Solution()
nums = [-1, 0, 1, 2, -1, -4]
result = solution.threeSum(nums)
print(f"{result}")