-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.java
More file actions
71 lines (67 loc) · 1.53 KB
/
NextPermutation.java
File metadata and controls
71 lines (67 loc) · 1.53 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Arrays;
/*
* https://leetcode.com/problems/next-permutation/
*/
public class NextPermutation {
public void nextPermutation(int[] nums) {
int lastIncrementalIndex = -1;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
lastIncrementalIndex = i;
break;
}
}
System.out.println(lastIncrementalIndex);
if (lastIncrementalIndex == -1) {
Arrays.sort(nums);
return;
}
int replacementIndex = nums.length - 1;
for (int i = lastIncrementalIndex + 1; i < nums.length; i++) {
if (nums[i] <= nums[lastIncrementalIndex]) {
replacementIndex = i - 1;
break;
}
}
System.out.println(replacementIndex);
swap(nums, lastIncrementalIndex, replacementIndex);
Arrays.sort(nums, lastIncrementalIndex + 1, nums.length);
System.out.println(Arrays.toString(nums));
}
private void swap(int[] nums, int index1, int index2) {
nums[index1] ^= nums[index2];
nums[index2] ^= nums[index1];
nums[index1] ^= nums[index2];
}
public static void main(String[] args) {
new NextPermutation().nextPermutation(new int[]{1, 5, 1}); // 1,3,2
}
}
//12345
//12354
//12435
//12453
//12534
//12543
//13245
//13254
//13425
//13452
//13524
//13542
//14235
//14253
//14325
//14352
//14523
//14532
//15234
//15243
//15324
//15342
//15423
//15432
//21345
//
//25431 35421
//31245