File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Problem link:https://leetcode.com/problems/3sum/description/
2
+
3
+ class Solution {
4
+ public:
5
+ vector<vector<int >> threeSum (vector<int >& nums) {
6
+ sort (nums.begin (),nums.end ());
7
+ int n=nums.size ();
8
+ vector<vector<int >> ans;
9
+ set<vector<int >> st;
10
+ for (int i=0 ;i<n;i++){
11
+ vector<int > check;
12
+ int find=-(nums[i]);
13
+ int l=i+1 ,r=n-1 ;
14
+ while (r>l){
15
+ int sum=(nums[l]+nums[r]);
16
+ if (sum==find){
17
+ check.push_back (nums[i]);
18
+ check.push_back (nums[l]);
19
+ check.push_back (nums[r]);
20
+ st.insert (check);
21
+ if (check.size ()==3 ){
22
+ check.clear ();
23
+ }
24
+ l++;r--;
25
+ }
26
+ else if (sum>find){
27
+ r--;
28
+ }
29
+ else {
30
+ l++;
31
+ }
32
+ }
33
+ }
34
+
35
+ for (auto el:st){
36
+ vector<int > check1;
37
+ for (int i=0 ;i<el.size ();i++){
38
+ check1.push_back (el[i]);
39
+ }
40
+ ans.push_back (check1);
41
+ }
42
+ return ans;
43
+
44
+ }
45
+ };
You can’t perform that action at this time.
0 commit comments