llllmz

导航

406. 根据身高重建队列c

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
typedef struct node{
    int h;
    int n;
}students;

void insert(int** array,int n,int index,students num){
    for(int i=n;i>index;i--){
        array[i][0]=array[i-1][0];
        array[i][1]=array[i-1][1];
    }
    array[index][0]=num.h;
    array[index][1]=num.n;
}

int cmp(const void* a,const void* b){
    students* x=(students*)a;
    students* y=(students*)b;
    if(x->h==y->h){
        return x->n-y->n;
    }
    return y->h-x->h;
}

int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {
    *returnSize=peopleSize;
    *peopleColSize=2;
    int* column=(int*)malloc(sizeof(int)*peopleSize);
    for(int i=0;i<peopleSize;i++) column[i]=2;
    students* s=(students*)malloc(sizeof(students)*peopleSize);
    for(int i=0;i<peopleSize;i++){
        s[i].h=people[i][0];
        s[i].n=people[i][1];    
    }
    qsort(s,peopleSize,sizeof(students),cmp);
    int** array=(int**)malloc(sizeof(int*)*peopleSize);
    for(int i=0;i<peopleSize;i++) array[i]=(int*)malloc(sizeof(int)*2);
    for(int i=0;i<peopleSize;i++){
        printf("%d",i);
        insert(array,i,s[i].n,s[i]);
    }
    *returnColumnSizes=column;
    return array;
}

posted on 2024-03-17 14:49  神奇的萝卜丝  阅读(21)  评论(0)    收藏  举报