forked from lccycc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
30 lines (29 loc) · 750 Bytes
/
Permutations.cpp
File metadata and controls
30 lines (29 loc) · 750 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
29
30
/*
sort them first
*/
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > res;
sort(num.begin(), num.end());
int n= num.size();
res.push_back(num);
if (n < 2) return res;
while (true){
int i = n-2;
while (i>=0 && num[i] >= num[i+1]) i--;
if (i>=0){
int j= n-1;
while (num[j]<=num[i]){
j--;
}
swap(num[i], num[j]);
for (int l = i+1, r = n-1; l<r; l++, r--)
swap(num[l], num[r]);
res.push_back(num);
}else
break;
}
return res;
}
};