-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-permutation.java
More file actions
40 lines (34 loc) · 1.1 KB
/
Copy pathnext-permutation.java
File metadata and controls
40 lines (34 loc) · 1.1 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
class Solution {
public void nextPermutation(int[] nums) {
int n = nums.length, pivot = -1;
// 1️⃣ Find pivot — first decreasing element from the right
for (int i = n - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
pivot = i;
break;
}
}
// 2️⃣ If no pivot found, reverse entire array (last permutation → first)
if (pivot == -1) {
reverse(nums, 0, n - 1);
return;
}
// 3️⃣ Find rightmost element greater than pivot, then swap
for (int i = n - 1; i > pivot; i--) {
if (nums[i] > nums[pivot]) {
swap(nums, i, pivot);
break;
}
}
// 4️⃣ Reverse suffix to make it smallest possible
reverse(nums, pivot + 1, n - 1);
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
private void reverse(int[] nums, int l, int r) {
while (l < r) swap(nums, l++, r--);
}
}