File tree 1 file changed +25
-1
lines changed
1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change 2
2
// Space: O(1)
3
3
4
4
class Solution {
5
+ public:
6
+ void nextPermutation (vector<int >& nums) {
7
+ int pivot = -1 ;
8
+ for (int i = nums.size () - 2 ; i >= 0 ; --i) {
9
+ if (nums[i] < nums[i + 1 ]) {
10
+ pivot = i;
11
+ break ;
12
+ }
13
+ }
14
+ if (pivot == -1 ) {
15
+ reverse (begin (nums), end (nums));
16
+ return ;
17
+ }
18
+ for (int i = nums.size () - 1 ; i > pivot; --i) {
19
+ if (nums[i] > nums[pivot]) {
20
+ swap (nums[pivot], nums[i]);
21
+ break ;
22
+ }
23
+ }
24
+ reverse (begin (nums) + pivot + 1 , end (nums));
25
+ }
26
+ };
27
+
28
+ class Solution2 {
5
29
public:
6
30
void nextPermutation (vector<int > &num) {
7
31
nextPermutation (num.begin (), num.end ());
@@ -35,7 +59,7 @@ class Solution {
35
59
}
36
60
};
37
61
38
- class Solution2 {
62
+ class Solution3 {
39
63
public:
40
64
void nextPermutation (vector<int > &num) {
41
65
next_permutation (num.begin (), num.end ());
You can’t perform that action at this time.
0 commit comments