10 lines Java using lambda expression

  • 0
    public int[][] reconstructQueue(int[][] people) {
            if(people==null || people.length<2) return people;
            Arrays.sort(people, Comparator.<int[]>comparingInt(p->-p[0]).thenComparingInt(p->p[1]));
            List<int[]>res = new LinkedList<>();
            for(int i=0;i<people.length;i++){
            	res.add(people[i][1], people[i]);
            int[][]result = new int[people.length][2];
            int i=0;
            for(int []p: res) { result[i++]=p; }
            return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.