Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c256bb3

Browse files
15. 3Sum
1 parent 4f4e094 commit c256bb3

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

‎TwoPointers/Medium/15_3Sum.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''
2+
15. 3Sum
3+
4+
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.
5+
Notice that the solution set must not contain duplicate triplets.
6+
7+
Example 1:
8+
Input: nums = [-1,0,1,2,-1,-4]
9+
Output: [[-1,-1,2],[-1,0,1]]
10+
Example 2:
11+
Input: nums = []
12+
Output: []
13+
Example 3:
14+
Input: nums = [0]
15+
Output: []
16+
'''
17+
18+
# Brute Force
19+
# Time Complexity: O(n^3)
20+
# Space Complexity: O(m)
21+
22+
'''
23+
In the brute force approach, we use three nested loops to iterate through all possible triplets in the array.
24+
We check if the sum of each triplet is equal to zero and if it is, we add it to the result set.
25+
The result set is used to avoid duplicates.
26+
We convert the triplet to a tuple and add it to the set.
27+
Finally, we convert the set back to a list of lists before returning it.
28+
'''
29+
30+
class Solution:
31+
def threeSum(self, nums):
32+
res=set()
33+
for i in range(len(nums)):
34+
for j in range(i+1,len(nums)):
35+
for k in range(j+1,len(nums)):
36+
if nums[i]+nums[j]+nums[k]==0:
37+
res.add(tuple(sorted([nums[i],nums[j],nums[k]])))
38+
39+
return [list(triplet) for triplet in res]
40+
41+
# Two Pointers
42+
# Time Complexity: O(n^2)
43+
# Space Complexity: O(1) or O(n) depending on the sorting algorithm. O(m) for the result list.
44+
45+
'''
46+
In the two pointers approach, we first sort the array.
47+
Then we iterate through the array and for each element, we use two pointers to find the other two elements that sum to zero.
48+
We set the left pointer to the next element and the right pointer to the last element.
49+
We check if the sum of the three elements is equal to zero. If it is, we add it to the result list.
50+
If the sum is greater than zero, we move the right pointer to the left. If the sum is less than zero, we move the left pointer to the right.
51+
We also check for duplicates by skipping over elements that are the same as the previous element.
52+
We continue this process until the left pointer is less than the right pointer.
53+
'''
54+
55+
class Solution:
56+
def threeSum(self, nums):
57+
nums.sort()
58+
res=[]
59+
for i in range(len(nums)):
60+
if i > 0 and nums[i] == nums[i - 1]:
61+
continue
62+
63+
l=i+1
64+
r=len(nums)-1
65+
while l<r:
66+
crsum = nums[i]+nums[l]+nums[r]
67+
if crsum==0:
68+
res.append([nums[i],nums[l],nums[r]])
69+
l+=1
70+
r-=1
71+
while l<r and nums[l]==nums[l-1]:
72+
l+=1
73+
while l<r and nums[r]==nums[r+1]:
74+
r-=1
75+
elif crsum>0:
76+
r-=1
77+
else:
78+
l+=1
79+
return res
80+
81+
sol = Solution()
82+
nums = [-1,0,1,2,-1,-4]
83+
print(sol.threeSum(nums))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /