-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNextPermutation.java
More file actions
49 lines (42 loc) · 1.3 KB
/
NextPermutation.java
File metadata and controls
49 lines (42 loc) · 1.3 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
package array;
/**
* Description: https://leetcode.com/problems/next-permutation
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(1)
*/
public class NextPermutation {
public void nextPermutation(int[] nums) {
int firstDecreasing = nums.length - 2;
// 1 5 8 [4] 7 6 5 3 1
while (firstDecreasing >= 0
&& nums[firstDecreasing] >= nums[firstDecreasing + 1]) {
firstDecreasing--;
}
if (firstDecreasing >= 0) {
// 1 5 8 [4] 7 6 [5] 3 1
int firstGreater = nums.length - 1;
while (nums[firstGreater] <= nums[firstDecreasing]) {
firstGreater--;
}
// 1 5 8 [4] 7 6 [5] 3 1 -> 1 5 8 [5] 7 6 [4] 3 1
swap(nums, firstDecreasing, firstGreater);
}
// 1 5 8 5 [7 6 4 3 1] -> 1 5 8 5 [1 3 4 6 7]
reverseStartingFrom(nums, firstDecreasing + 1);
}
private void reverseStartingFrom(int[] nums, int start) {
int left = start;
int right = nums.length - 1;
while (left < right) {
swap(nums, left, right);
left++;
right--;
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}