-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.java
More file actions
77 lines (72 loc) · 2.74 KB
/
Copy pathNextPermutation.java
File metadata and controls
77 lines (72 loc) · 2.74 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
72
73
74
75
76
77
import java.util.Arrays;
/**
* Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
* If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending
* order). The replacement must be in-place, do not allocate extra memory.
*
* Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand
* column.
* 1,2,3 ¡ú 1,3,2
* 3,2,1 ¡ú 1,2,3
* 1,1,5 ¡ú 1,5,1
*
* @author joshluo
*
* Resulution example:
* 7869872 -> 7872689
* 0. n = A.length
* 1. From A[n-1] to start, find the first A[i-1] = S < A[i], which means A[i] - A[n-1] are in descending order.
* 2. From A[n-1] to A[i], find the first A[s] = T > S
* 3. Swap S and T, then the array from A[i] to A[n-1] will be descending order
* 4. Sort the values after A[i] to A[n-1] to ascending order. Divide and conquer to do swap.
*
*/
public class NextPermutation {
public void nextPermutation(int[] num) {
if (num == null || num.length <= 1) {
return;
}
int n = num.length;
// 1. From A[n-1] to start, find the first A[i-1] = S < A[i], which means A[i] - A[n-1] are in descending order.
int indexS = -1;
for (int i = n - 1; i > 0; i--) {
if (num[i - 1] < num[i]) {
indexS = i - 1;
break;
}
}
if (indexS >= 0) {
// 2. From A[n-1] to A[i], find the first A[s] = T > S
int indexT = -1;
for (int i = n - 1; i > indexS; i--) {
if (num[i] > num[indexS]) {
indexT = i;
break;
}
}
// 3. Swap S and T
if (indexT >= 0) {
swapTwoNumbersInArray(num, indexS, indexT);
}
}
reverseDescendingToAscending(num, indexS + 1, num.length - 1);
}
private void swapTwoNumbersInArray(int[] num, int index1, int index2) {
int temp = num[index1];
num[index1] = num[index2];
num[index2] = temp;
}
private void reverseDescendingToAscending(int[] num, int startIndex, int endIndex) {
if (startIndex >= endIndex || startIndex < 0 || endIndex > num.length - 1) {
return;
}
swapTwoNumbersInArray(num, startIndex, endIndex);
reverseDescendingToAscending(num, startIndex + 1, endIndex - 1);
}
public static void main(String[] args) {
NextPermutation testCLass = new NextPermutation();
int[] testCase = { 1, 2 };
testCLass.nextPermutation(testCase);
System.out.println(Arrays.toString(testCase));
}
}