public void nextPermutation(int[] num) {
for(int i = num.length1; i > 0; i) {
if(num[i1] < num[i]) {
Arrays.sort(num, i, num.length);
for(int j = i; j < num.length; j++) {
if(num[j] > num[i1]) {
// swap num[i1] and num[j]
num[i1] = num[i1] + num[j];
num[j] = num[i1]  num[j];
num[i1] = num[i1]  num[j];
return;
}
}
}
}
Arrays.sort(num);
}
Simple Java solution for your reference


Since nums array is definitely descending order when you call arrays#sort, you would better implement your own reverse function rather than sort to reduce your runtime.
public void reverse(int[] nums, int from, int to) { int len = to  from; for (int i=0; i<len/2; i++) { int temp = nums[from+i]; nums[from+i] = nums[to1i]; nums[to1i] = temp; } }