public class Solution {
public void nextPermutation(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int len = nums.length;
for (int i = len - 2; i >= 0; i--) {
if (nums[i + 1] > nums[i]) {
int j;
for (j = len - 1; j > i; j--) {
if (nums[j] > nums[i]) {
break;
}
}
swap(nums, i, j);
reverse(nums, i + 1, len - 1);
return;
}
}
reverse(nums, 0, len - 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 start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
swap(nums, i, j);
}
}
}