题目大意:有一队人,每个人由一个(h,k)表示,h表示高度,k表示前面高于或等于自己的人个人。给一个打乱了的队列,求出正确的序列。
思路:
1.每次从队列中找出k=0,再在其中选出h最小的,加入到res队列中;然后对剩余队列做相应的处理;再循环,即可求出全部队列,类似于递归。176ms
2.先把队列按h从大到小,h相同则k从小到大的顺序排列,然后依次将每个人插入到res队列。83ms
CODE:
1.
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** reconstructQueue(int** people, int peopleRowSize, int peopleColSize, int** columnSizes, int* returnSize) {
if(peopleRowSize<1||peopleColSize<1) {columnSizes=NULL;*returnSize=0;return NULL;}
*returnSize=peopleRowSize;
int i,j,k,h,**res;
*columnSizes=(int*)malloc(sizeof(int)*peopleRowSize);
res=malloc(sizeof(int*)*peopleRowSize);
for(i=0;i=res[i][0])res[i][1]++;
}
people[k][1]=-1;
for(j=0;j2.
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** reconstructQueue(int** people, int peopleRowSize, int peopleColSize, int** columnSizes, int* returnSize) {
if(peopleRowSize<1||peopleColSize<1) {columnSizes=NULL;*returnSize=0;return NULL;}
*returnSize=peopleRowSize;
int i,j,k,**res;
*columnSizes=(int*)malloc(sizeof(int)*peopleRowSize);
res=malloc(sizeof(int*)*peopleRowSize);
for(i=0;ipeople[j][1]){
k=people[i][1],people[i][1]=people[j][1],people[j][1]=k;
}
}
}
//以上为排序
res[0][0]=people[0][0];res[0][1]=people[0][1];
for(i=1;ij){
if(res[k][0]>=people[i][0]) j++;
k++;
}
for(j=i;j>k;j--){
res[j][0]=res[j-1][0];
res[j][1]=res[j-1][1];
}
res[k][0]=people[i][0];
res[k][1]=people[i][1];
}
return res;
}

被折叠的 条评论
为什么被折叠?



