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().
 */
int divide(int** people,int head,int tail){
    int th=people[head][0],tk=people[head][1];
    while(head<tail){
        while(head<tail && (people[tail][0] < th || (people[tail][0]==th && people[tail][1] > tk ) )) tail--;
        if(head<tail){
            people[head][0]=people[tail][0];
            people[head][1]=people[tail][1];
            head++;
        }
        while(head<tail && (people[head][0] > th ||(people[head][0]==th && people[head][1] <tk ) ) ) head++;
        if(head<tail){
            people[tail][0]=people[head][0];
            people[tail][1]=people[head][1];
            tail--;
        }
    }
    people[head][0]=th;
    people[head][1]=tk;
    return head;
}

void quicksort(int** people,int head,int tail){
    if(head>=tail) return;
    int t=divide(people,head,tail);
    if(t>head) quicksort(people,head,t-1);
    if(t<tail) quicksort(people,t+1,tail);
}

void insert(int** array,int head,int n,int h,int k){
    for(int i=n;i>head;i--){
        array[i][0]=array[i-1][0];
        array[i][1]=array[i-1][1];
    }
    array[head][0]=h;
    array[head][1]=k;
}

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

结果;

posted on 2024-03-11 16:24  神奇的萝卜丝  阅读(9)  评论(0)    收藏  举报