-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThree Sum.cpp
More file actions
40 lines (35 loc) · 1.21 KB
/
Copy pathThree Sum.cpp
File metadata and controls
40 lines (35 loc) · 1.21 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
// Problem: 3Sum
// Given an array nums, find all unique triplets [a,b,c] such that a+b+c = 0.
//
// Approach:
// 1. Sort the array to handle duplicates easily.
// 2. Iterate through the array, fixing one element (nums[i]).
// 3. Use two pointers (j, k) to find pairs that sum with nums[i] to 0.
// 4. Skip duplicate elements for both i and j to avoid repeating triplets.
//
// Time Complexity: O(n^2)
// Space Complexity: O(1) excluding result storage.
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> ans;
sort(nums.begin(), nums.end());
for (int i = 0; i < n; i++) {
if (i > 0 && nums[i] == nums[i-1]) continue; // skip duplicate i
int j = i+1, k = n-1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum < 0) j++;
else if (sum > 0) k--;
else {
ans.push_back({nums[i], nums[j], nums[k]});
j++; k--;
// skip duplicate j
while (j < k && nums[j] == nums[j-1]) j++;
}
}
}
return ans;
}
};