class Solution {
public void nextPermutation(int[] nums) {
int start = 0;
for (int i = nums.length - 1; i >= 1; i--) {
int k = i+1;
if (nums[i] > nums[i - 1]) {
while(k<nums.length && nums[i-1]<nums[k]) k++;
swap(nums,i-1,k-1);
start = i;
break;
}
}
reverse(nums, start);
}
private void reverse(int[] nums, int start) {
int i = start, j = nums.length - 1;
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
private void swap(int[] nums, int x, int y) {
int t = nums[x];
nums[x] = nums[y];
nums[y] = t;
}
}