-
Notifications
You must be signed in to change notification settings - Fork 10
/
Next Permutation.java
66 lines (57 loc) · 2.45 KB
/
Next Permutation.java
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
/*
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
*/
//from right to left, find the first number that violates the increasing trend --> Partition Number
//from right to left, find the first number that is bigger than Partition Number --> Change Number
//swap Partition and Change Number
//reverse all the number on the right side of the partition index
public class Solution {
public void nextPermutation(int[] num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int length = num.length;
if (length == 0 || length == 1) return;
int partitionIndex = length, changeIndex = length;
for (int i = length - 1; i > 0; i--) {
if (num[i] > num[i - 1]) {
partitionIndex = i - 1;
break;
}
}
if (partitionIndex == length) {
for (int i = 0; i < length/2; i++) {
int temp = num[i];
num[i] = num[length - 1 - i];
num[length - 1 - i] = temp;
}
} else {
for (int i = length - 1; i >= 0; i--) {
if (num[i] > num[partitionIndex]) {
changeIndex = i;
int temp = num[changeIndex];
num[changeIndex] = num[partitionIndex];
num[partitionIndex]= temp;
break;
}
}
if (changeIndex == length) {
changeIndex = partitionIndex - 1;
int temp = num[changeIndex];
num[changeIndex] = num[partitionIndex];
num[partitionIndex]= temp;
} else {
for (int i = partitionIndex + 1; i < (length + partitionIndex + 1)/2; i++) {
int temp = num[i];
num[i] = num[length + partitionIndex - i];
num[length + partitionIndex - i] = temp;
}
}
}
}
}