1. Description
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, do not allocate extra memory.
2. Example
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
2. Code
import java.util.Arrays;
public class LeetCode0031 {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int max = nums[nums.length - 1];
for (int i = nums.length - 2; i >= 0; i--) {
if(nums[i] >= max) {
max = nums[i];
continue;
}
int moreMax = Integer.MAX_VALUE;
int index = -1;
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] > nums[i] && nums[j] < moreMax) {
moreMax = nums[j];
index = j;
}
}
nums[i] ^= nums[index];
nums[index] ^= nums[i];
nums[i] ^= nums[index];
Arrays.sort(nums, i + 1, nums.length);
return;
}
Arrays.sort(nums);
return;
}
public static void main(String[] args) {
LeetCode0031 leetcode = new LeetCode0031();
int[] nums = new int[] { 2, 3, 1 };
leetcode.nextPermutation(nums);
System.out.println(Arrays.toString(nums));
}
}
Comments | NOTHING