class Solution {
public int[][] reconstructQueue(int[][] people) {
int[][] res = new int[people.length][2];
Arrays.sort(people,new Comparator<int[]>(){
@Override
public int compare(int[] a, int[] b){
if(b[0] == a[0]){
return a[1] - b[1];
}
return b[0] - a[0];
}
});
for(int i = 0;i < people.length;i++){
int index = people[i][1];
for(int j = i; j > index;j--){
res[j] = res[j - 1];
}
res[index] = people[i];
}
return res;
}
}
根据身高排序,最高的排最前面,如果出现相等,按前面人数少的排前面,这样可以保证从前往后遍历,people[i][1]的值就是这个人在当前序列中该出现的位置,在他之后的人全部后移一位即可。