-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.java
More file actions
69 lines (57 loc) · 1.76 KB
/
Copy pathNextPermutation.java
File metadata and controls
69 lines (57 loc) · 1.76 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
/*
* Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers.
*
If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order.
The replacement must be in-place, do not allocate extra memory.
Examples:
1,2,3 ? 1,3,2
3,2,1 ? 1,2,3
1,1,5 ? 1,5,1
20, 50, 113 ? 20, 113, 50
Step 1: Find the largest index k, such that A[k]<A[k+1]. If not exist, this is the last permutation. (in this problem just sort the vector and return.)
Step 2: Find the largest index l, such that A[l]>A[k].
Step 3: Swap A[k] and A[l].
Step 4: Reverse A[k+1] to the end.
*/
import java.util.*;
public class NextPermutation{
public static void nextPermutation(ArrayList<Integer> A) {
int n = A.size();
int k = -1;
int l = 0;
for(int i = 0; i < n-1; i++){
if(A.get(i) < A.get(i+1))
k = i;
}
if(k == -1){
Collections.sort(A);
return;
}
for(int i = k+1; i < n; i++){
if(A.get(i) > A.get(k)){
l = i;
}
}
int temp = A.get(l);
A.set(l, A.get(k));
A.set(k, temp);
int j = k + 1;
int last = n-1;
while(j <= last){
temp = A.get(j);
A.set(j, A.get(last));
A.set(last, temp);
j++;
last--;
}
for(int i = 0; i < A.size(); i++)
System.out.print(A.get(i) + " ");
}
public static void main(String[] args){
ArrayList<Integer> A = new ArrayList<Integer>();
A.add(1);
A.add(2);
A.add(3);
nextPermutation(A);
}
}