File tree Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public void nextPermutation (int [] nums ) {
3
- if (nums .length <= 1 ) {
4
- return ;
5
- }
6
3
int idx = nums .length - 2 ;
7
- while (idx >= 0 && nums [idx ] > = nums [idx + 1 ]) {
4
+ while (idx >= 0 && nums [idx + 1 ] < = nums [idx ]) {
8
5
idx --;
9
6
}
10
7
if (idx >= 0 ) {
@@ -14,16 +11,21 @@ public void nextPermutation(int[] nums) {
14
11
}
15
12
swap (nums , idx , endIdx );
16
13
}
17
- int startIdx = idx + 1 ;
14
+ reverse (nums , idx + 1 );
15
+ }
16
+
17
+ private void reverse (int [] nums , int startIdx ) {
18
18
int endIdx = nums .length - 1 ;
19
19
while (startIdx < endIdx ) {
20
- swap (nums , startIdx ++, endIdx --);
20
+ swap (nums , startIdx , endIdx );
21
+ startIdx ++;
22
+ endIdx --;
21
23
}
22
24
}
23
25
24
- private void swap (int [] nums , int idxOne , int idxTwo ) {
25
- int temp = nums [idxOne ];
26
- nums [idxOne ] = nums [idxTwo ];
27
- nums [idxTwo ] = temp ;
26
+ private void swap (int [] nums , int i , int j ) {
27
+ int temp = nums [i ];
28
+ nums [i ] = nums [j ];
29
+ nums [j ] = temp ;
28
30
}
29
31
}
You can’t perform that action at this time.
0 commit comments