-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNextPermutation.java
More file actions
37 lines (34 loc) · 951 Bytes
/
NextPermutation.java
File metadata and controls
37 lines (34 loc) · 951 Bytes
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
public class NextPermutation {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length < 2) return;
int p = 0, q = 0;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
p = i;
break;
}
}
for (int i = nums.length - 1; i > p; i--) {
if (nums[i] > nums[p]) {
q = i;
break;
}
}
if (p == 0 && q == 0)
reverse(nums, 0, nums.length - 1);
else {
swap(nums, p, q);
reverse(nums, p + 1, nums.length - 1);
}
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start++, end--);
}
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}