-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46 全排列.cpp
More file actions
28 lines (27 loc) · 773 Bytes
/
46 全排列.cpp
File metadata and controls
28 lines (27 loc) · 773 Bytes
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
class Solution {
public:
void cal(vector<int>& dp,vector<int> &temp,vector<int> &factor,int k,int n){
int index;
for(int i=0;i<n;i++){
sort(dp.begin(),dp.end());
index=k/factor[n-i-1];
k=k%factor[n-i-1];
temp.push_back(dp[index]);
dp[index]=1000;
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<int> dp;
int n=nums.size();
vector<int> factor(n+1,1);
for(int i=1;i<n+1;i++) factor[i]=factor[i-1]*i;
vector<vector<int>> ret(factor[n]);
for(int i=0;i<factor[n];i++){
vector<int> temp;
dp=nums;
cal(dp,temp,factor,i,n);
ret[i]=temp;
}
return ret;
}
};