-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNextPermutation.java
More file actions
51 lines (46 loc) · 1.45 KB
/
NextPermutation.java
File metadata and controls
51 lines (46 loc) · 1.45 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
package leetcode;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* @author eko
*
* Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
*
* If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
*
* The replacement must be in-place and use only constant extra memory.
*
* Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
*
* 1,2,3 → 1,3,2
* 3,2,1 → 1,2,3
* 1,1,5 → 1,5,1
*
*/
public class NextPermutation {
public static void main(String[] args) {
int[] nums = new int[]{1, 3, 2};
new NextPermutation().nextPermutation(nums);
System.out.println(Arrays.stream(nums).boxed().collect(Collectors.toList()));
}
public void nextPermutation(int[] nums) {
int swap = -1;
for (int i = nums.length - 1; i > 0 ; i--) {
if (nums[i] > nums[i-1]) {
swap = i-1;
break;
}
}
if (swap >= 0) {
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[swap]) {
int temp = nums[i];
nums[i] = nums[swap];
nums[swap] = temp;
break;
}
}
}
Arrays.sort(nums, swap+1, nums.length);
}
}