-
Notifications
You must be signed in to change notification settings - Fork 0
/
0804.cpp
61 lines (55 loc) · 1.29 KB
/
0804.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> res;
int n;
void dfs(vector<int> cur,int index)
{
if(cur.empty() || index==cur.size())
{
sort(cur.begin(),cur.end());
unique(cur.begin(),cur.end());
res.push_back(cur);
return;
}
dfs(cur,index+1);
cur.erase(cur.begin()+index);
dfs(cur,index);
}
vector<vector<int>> subsets(vector<int>& nums)
{
n=nums.size();
dfs(nums,0);
return res;
}
// T15
bool unique(const vector<int>& a,const vector<int>& b)
{
if(a.size()!=b.size()) return false;
for(int i=0;i<a.size();i++)
if(a[i]!=b[i]) return false;
return true;
}
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> res;
int i=0,j=0;
for(;i<nums.size();i++)
{
for(;j<i;j++)
{
int temp=nums[i]+nums[j];
temp=-temp;
auto pos=find(nums.begin(),nums.end(),temp);
if(pos!=nums.end())
{
vector<int> one(3);
one[0]=(nums[i]);
one[1]=(nums[i]);
one[2]=temp;
sort(one.begin(),one.end());
res.push_back(one);
}
}
}
return res;
}