Skip to content

Commit

Permalink
Time: 2143 ms (5.01%), Space: 291.6 MB (7.08%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinavGor committed Sep 26, 2023
1 parent 66cd40f commit 5cc1501
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions 0015-3sum/0015-3sum.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
int n = nums.size();

if(nums.size() < 3)
return res;

set<vector<int>> res;
sort(begin(nums), end(nums));

for(int i = 0; i < nums.size(); i++){
int t = nums[i], l = i + 1, r = nums.size() - 1;
for(int i = 0; i < nums.size(); i++) {
int temp = nums[i];
int l = 0, r = nums.size() - 1;

while(l < r){
int sum = t + nums[l] + nums[r];
if(sum == 0){
res.push_back({t, nums[l], nums[r]});
while(l <r && nums[l] == nums[l + 1]) l++;
while(l < r && nums[r] == nums[r - 1]) r--;

while(l < r) {
int sum = nums[l] + nums[r] + temp;
// cout<<<<temp<<" "<<nums[l]sum<<endl;
if(sum == 0 && l != i && r != i) {
vector<int> temp_v = {nums[l], nums[r], temp};
sort(begin(temp_v), end(temp_v));
res.insert(temp_v);
l++;
r--;
}else if(sum > 0){
r--;
}else{
} else if(sum < 0) {
l++;
} else if (sum > 0) {
r--;
} else if(l == i || r == i) {
break;
}
}
while(i + 1 < n && nums[i] == nums[i + 1]) i++;
}

return res;
vector<vector<int>> res_v;

for(vector<int> v: res) {
res_v.push_back(v);
}

return res_v;
}
};

0 comments on commit 5cc1501

Please sign in to comment.