Untitled
unknown
plain_text
a year ago
1.2 kB
3
Indexable
Never
class Solution { public: //approach ->picking up the first element and using two pointers in consective elements //tc->o(n2) //sc-o(1) vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(),nums.end()); int e=nums.size()-1; vector<vector<int>>ans; for(int i=0;i<nums.size();i++) { if(i!=0&&i<nums.size()&&nums[i]==nums[i-1]) { continue; } // while(i!=0&&i<nums.size()&&nums[i]==nums[i-1]) // { // i++; // } int target=-nums[i]; int j=i+1; int e=nums.size()-1; while(j<e) { long long int s=nums[j]+nums[e]; if(s==target) { ans.push_back({nums[i],nums[j],nums[e]}); j++; while(j<e&&nums[j]==nums[j-1]) j++; e--; while(j<e&&nums[e]==nums[e+1]) e--; } else if(s>target) e--; else j++; } } return ans; } };