-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31-next-permutation.cpp
More file actions
43 lines (41 loc) · 1.06 KB
/
Copy path31-next-permutation.cpp
File metadata and controls
43 lines (41 loc) · 1.06 KB
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
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int l = nums.size();
int i = l - 1;
while (i > 0) {
if (nums[i] <= nums[i - 1]) {
i--;
continue;
}
int j = i;
while (j != l && nums[i - 1] < nums[j]) {
j++;
}
int temp = nums[i - 1];
nums[i - 1] = nums[j - 1];
nums[j - 1] = temp;
int start = i;
int end = l - 1;
while (start < end) {
int t = nums[start];
nums[start] = nums[end];
nums[end] = t;
start++;
end--;
}
break;
}
if (i == 0) {
int start = 0;
int end = l - 1;
while (start < end) {
int t = nums[start];
nums[start] = nums[end];
nums[end] = t;
start++;
end--;
}
}
}
};