美文网首页
LeetCode 第406题:根据身高重建队列

LeetCode 第406题:根据身高重建队列

作者: 放开那个BUG | 来源:发表于2021-06-11 14:54 被阅读0次

1、前言

题目描述

2、思路

根据 person 数组,按照 person[0] 降序排列,再按照 person[1]生序排列。

3、代码

public class Q406_ReconstructQueue {

    /**
     * 1、先按照身高降序排列,如果身高相同,那么按照前面出现的次数生序排序
     * 2、排序完成之后,按照前面出现的次数作为索引插入到集合中(可以自己操作一遍)
     * @param people
     * @return
     */
    public int[][] reconstructQueue(int[][] people) {
        if(people == null || people.length == 0){
            return new int[][]{};
        }

        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                int value = o2[0] - o1[0];
                if(value == 0){
                    value = o1[1] - o2[1];
                }
                return value;
            }
        });

        LinkedList<int[]> list = new LinkedList<>();
        for (int[] person : people) {
            list.add(person[1], person);
        }

        return list.toArray(people);
    }

    public static void main(String[] args) {
        int[][] people = {
                {7, 0},{4,4},{7,1},{5,0},{6,1},{5,2}
        };

        // fast、fast、fast、fast!!!!
        int[][] res = new Q406_ReconstructQueue().reconstructQueue(people);
        for (int[] re : res) {
            System.out.println(re[0] + ":" + re[1]);
        }
    }
}

相关文章

网友评论

      本文标题:LeetCode 第406题:根据身高重建队列

      本文链接:https://www.haomeiwen.com/subject/tbjreltx.html