406. 根据身高重建队列

406. 根据身高重建队列

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]的值就是这个人在当前序列中该出现的位置,在他之后的人全部后移一位即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值