一、题目
二、代码
class Solution
{
public int[][] reconstructQueue(int[][] people)
{
//定完身高 一切清晰明了
//默认 a-b升序
// b - a 降序
int i;
int length;
Arrays.sort(people,(a,b)->
{
if(a[0]==b[0]) return a[1]-b[1];
return b[0]-a[0];
}
);
LinkedList<int[]> list = new LinkedList<>();
for(int[] p:people)
{
list.add(p[1],p);
}
return list.toArray(new int[people.length][]);
}
}
三、运行结果
四、附录
二刷
class Solution
{
//高的是可以确定的 先按照由高到低排 然后插入
public int[][] reconstructQueue(int[][] people)
{
//a-b升序
//下面排序是升高降序 排名升序
Arrays.sort(people,(a,b)->
{
if(a[0] == b[0] ) return a[1] - b[1];
return b[0]-a[0];
}
);
int i,j;
int length = people.length;
// int max = 0;
// for(i=0;i<length;i++)
// {
// System.out.print(" i "+people[i][0]);
// System.out.println(" "+people[i][1]);
// //if(max<people[i][1]) max = people[i][1];
// }
List<int[]> res = new LinkedList<>();
for(int[] temp:people) res.add(temp[1],temp);
int[][] res_array = new int[length][2];
for(i=0;i<length;i++)
{
res_array[i][0] = res.get(i)[0];
res_array[i][1] = res.get(i)[1];
}
//其实就是int[]的集合
//从后向前 从小到大
return res_array;
}
}