-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutations.cpp
48 lines (45 loc) · 977 Bytes
/
Permutations.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
#include<iostream>
#include<vector>
using namespace std;
void cur(vector<vector<int> >& re,vector<int>& cur_re,vector<int>& left,int dep)
{
if (dep==0)
{
re.push_back(cur_re);
return;
}
for (int i=0;i<left.size();i++)
{
cur_re.push_back(left[i]);
int tmp=left[i];
left.erase(left.begin()+i);
cur(re,cur_re,left,dep-1);
cur_re.pop_back();
left.insert(left.begin()+i,tmp);
}
}
vector<vector<int> > permute(vector<int>& nums)
{
vector<vector<int> > re;
vector<int> cur_re;
if (nums.size()==0)
return re;
cur(re,cur_re,nums,nums.size());
return re;
}
int main()
{
vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
vector<vector<int> > re;
re=permute(nums);
for (int i=0;i<re.size();i++)
{
for (int j=0;j<re[i].size();j++)
cout<<re[i][j]<<" ";
cout<<endl;
}
return 0;
}