-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext Permutation.cpp
More file actions
32 lines (28 loc) · 985 Bytes
/
Copy pathNext Permutation.cpp
File metadata and controls
32 lines (28 loc) · 985 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
31
32
//permutation 排列组合
// if the numbers are in descending order, return the reverse order. Else find the first element that breaks the order, the position is j. swap it with the smallest num that is larger than the nums[j]
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size = nums.size();
if(size == 0 || size == 1)
return nums;
int pos1, pos2;
for(pos1 = size - 2; pos1 >= 0; pos1--){
if(nums[pos1] < nums[pos1 + 1]){
break;
}
}
if(pos1 == -1){ // no num breaks the law
sort(nums.begin(), nums.end());
return nums;
}
for(int pos2 = size - 1; pos2 > pos1; pos2--){
if(nums[pos2] > nums[pos1]){
swap(nums[pos1], nums[pos2]);
break;
}
}
sort(nums.begin()+pos1+1,nums.end());
return nums;
}
};