-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation 2.cpp
More file actions
45 lines (44 loc) · 1.29 KB
/
Permutation 2.cpp
File metadata and controls
45 lines (44 loc) · 1.29 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
41
42
43
44
45
class Solution {
public:
void DFS(int depth, int size, vector<bool> &hash, vector<int> num, vector<int> &result, vector<vector<int> > &ans)
{
if(depth==size)
{
ans.push_back(result);
return ;
}
int left=0, right;
while(left<size)
{
if(hash[left]==false)
{
result[depth]=num[left];
hash[left]=true;
DFS(depth+1, size, hash, num, result, ans);
hash[left]=false;
right=left+1;
while(right<size&&num[left]==num[right])
right++;
left=right;
}
else
left++;
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ans;
vector<int> result;
vector<bool> hash;
if(num.size()==0)
return ans;
sort(num.begin(), num.end());
int size=num.size();
result.resize(size);
for(int i=0;i<size;i++)
hash.push_back(false);
DFS(0, size, hash, num, result, ans);
return ans;
}
};