C语言 | Leetcode C语言题解之第406题根据身高重建队列
题目:
题解:
int cmp(const void* _a, const void* _b) {
int *a = *(int**)_a, *b = *(int**)_b;
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
}
int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {
qsort(people, peopleSize, sizeof(int*), cmp);
int** ans = malloc(sizeof(int*) * peopleSize);
*returnSize = 0;
*returnColumnSizes = malloc(sizeof(int) * peopleSize);
for (int i = 0; i < peopleSize; i++) {
(*returnColumnSizes)[i] = 2;
}
for (int i = 0; i < peopleSize; ++i) {
int* person = people[i];
(*returnSize)++;
for (int j = (*returnSize) - 1; j > person[1]; j--) {
ans[j] = ans[j - 1];
}
int* tmp = malloc(sizeof(int) * 2);
tmp[0] = person[0], tmp[1] = person[1];
ans[person[1]] = tmp;
}
return ans;
}