Leetcode - Queue Reconstruction by Height

My code:

public class Solution {
    public int[][] reconstructQueue(int[][] people) {
        if (people == null || people.length == 0 || people[0].length == 0) {
            return people;
        }
        
        Arrays.sort(people, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                if (o1[0] != o2[0]) {
                    return o2[0] - o1[0];
                }
                else {
                    return o1[1] - o2[1];
                }
            } 
        });
        
        List<int[]> list = new ArrayList<int[]>();
        for (int i = 0; i < people.length; i++) {
            list.add(people[i][1], people[i]);
        }
        
        int[][] ret = new int[people.length][people[0].length];
        for (int i = 0; i < ret.length; i++) {
            ret[i][0] = list.get(i)[0];
            ret[i][1] = list.get(i)[1];
        }
        
        return ret;
    }
}

reference:
https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution/4

想到了排序,但是具体怎么排,排了之后怎么做,还是不清楚。
这个解法真的很简洁。

Anyway, Good luck, Richardo! -- 10/13/2016

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容