File tree 1 file changed +9
-9
lines changed
1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -2,9 +2,11 @@ class Solution {
2
2
public:
3
3
void nextPermutation (vector<int >& nums) {
4
4
5
- int i1,i2;
5
+ int i1;
6
+ int i2;
6
7
bool hasPermutation = false ;
7
- for (int i=nums.size ()-1 ;i>0 ;i--){
8
+
9
+ for (int i=nums.size ()-1 ; i>0 ; i--){
8
10
if (nums[i-1 ]<nums[i]){
9
11
i1 = i-1 ;
10
12
i2 = i;
@@ -15,19 +17,17 @@ class Solution {
15
17
16
18
if (hasPermutation){
17
19
int j=i2;
18
- for (int i=nums.size ()-1 ;i>i1;i--){
20
+ for (int i=nums.size ()-1 ; i>i1; i--){
19
21
if (nums[i]>nums[i1]){
20
22
j=i;
21
23
break ;
22
24
}
23
25
}
24
- swap (nums[i1],nums[j]);
25
- reverse (nums.begin ()+i1+1 ,nums.end ());
26
+ swap (nums[i1], nums[j]);
27
+ reverse (nums.begin ()+i1+1 , nums.end ());
26
28
}else {
27
- sort (nums.begin (),nums.end ());
28
- }
29
- for (auto i:nums){
30
- cout << i << " " ;
29
+ sort (nums.begin (), nums.end ());
31
30
}
31
+
32
32
}
33
33
};
You can’t perform that action at this time.
0 commit comments