forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
60. Permutation Sequence.java
55 lines (47 loc) · 1.57 KB
/
60. Permutation Sequence.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
class Solution {
public String getPermutation(int n, int k) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
int count = 1;
while (count < k) {
nextGreaterPermutation(nums);
count++;
}
StringBuilder result = new StringBuilder();
for (int num : nums) result.append(num);
return result.toString();
}
private void nextGreaterPermutation(int[] nums) {
int n = nums.length;
int i = n - 1;
// Find the fist digit(pivot) from the last which is smaller than the next
while (i > 0 && nums[i - 1] >= nums[i]) {
i--;
}
// if i = 0, means all the digits are in descending order, no next greater permutation
if (i > 0) {
// Find the digit just greater than the pivot digit
int x = nums[i - 1];
int minIndex = n - 1;
while (nums[minIndex] <= x) {
minIndex--;
}
// Swap the pivot digit with the next greater digit
swap(nums, i - 1, minIndex);
// Need to reverse the remaing digits for next greater permutation
reverse(nums, i, n - 1);
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i++, j--);
}
}
}