mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 0 ms (100.00%), Space: 6.2 MB (46.95%) - LeetHub
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
int cmpInt(const void *a, const void *b) {
|
||||
const int *ia = a;
|
||||
const int *ib = b;
|
||||
return *ia - *ib;
|
||||
}
|
||||
|
||||
void nextPermutation(int* nums, int numsSize){
|
||||
int firstDecrI = -1;
|
||||
for (int i = numsSize - 2; i >= 0; --i) {
|
||||
if (nums[i] < nums[i + 1]) {
|
||||
firstDecrI = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Last permutation edge case
|
||||
if (firstDecrI == - 1) {
|
||||
qsort(nums, numsSize, sizeof(int), cmpInt);
|
||||
return;
|
||||
}
|
||||
|
||||
// place next largest num into nums[firstDecrI]
|
||||
int nextHighest = INT_MAX;
|
||||
int nextHighestI = -1;
|
||||
for (int i = firstDecrI + 1; i < numsSize; ++i) {
|
||||
if (nums[firstDecrI] < nums[i] && nums[i] < nextHighest) {
|
||||
nextHighest = nums[i];
|
||||
nextHighestI = i;
|
||||
}
|
||||
}
|
||||
int tmp = nums[firstDecrI];
|
||||
nums[firstDecrI] = nums[nextHighestI];
|
||||
nums[nextHighestI] = tmp;
|
||||
|
||||
// sort nums[firstDecrI + 1...numsSize - 1]
|
||||
qsort(nums + firstDecrI + 1, numsSize - firstDecrI - 1, sizeof(int), cmpInt);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user