-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations.cpp
52 lines (47 loc) · 1.13 KB
/
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
49
50
51
52
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
int length=nums.size();
int times=fac(length);
for(int t=0;t<=times-1;t++)
{
nextPermutation(nums);
result.push_back(nums);
}
return result;
}
void nextPermutation(vector<int>& nums) {
int length=nums.size();
if(length<=1) return ;
//找到升序点
int i=length-2;
while(i>=0 && nums[i]>=nums[i+1]) i--;
//将升序点与后面比它大的最小的数值交换
if(i>=0)
{
int j=length-1;
while(j>i && nums[i]>=nums[j]) j--;
swap(nums[i],nums[j]);
reverse(nums,i+1,length-1);
}
else
reverse(nums,0,length-1);
// return nums;
}
void reverse(vector<int> &nums,int i,int j)
{
while(i<j)
{
swap(nums[i],nums[j]);
i++;
j--;
}
}
int fac(int n)
{
if(n==1) return 1;
else
{return n*fac(n-1);}
}
};