这题的关键点在于对数组的重排序方法,高度先由高到低排列不会影响第二个参数,因为list.add的方法在指定index后面插入,因此对于同高的人来说需要对第二个参数由低到高排,具体代码如下

 public class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return o1[0] == o2[0]? o1[1] - o2[1] : o2[0] - o1[0];
}
});
List<int[]> pList = new ArrayList<int[]>();
for (int i = 0; i < people.length; i++) {
pList.add(people[i][1], new int[]{people[i][0], people[i][1]});
}
int[][] ans = new int[people.length][2];
for (int i = 0; i < pList.size(); i++) {
ans[i] = pList.get(i);
}
return ans;
}
}

int[][] ans = (int[][])pList.toArray();会报错,只能遍历赋值了

LeetCode: Queue Reconstruction by Height的更多相关文章

  1. [LeetCode] Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  2. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  3. LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46

    406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...

  4. LN : leetcode 406 Queue Reconstruction by Height

    lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...

  5. LC 406. Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  6. [LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  7. 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  9. [leetcode] 406. Queue Reconstruction by Height (medium)

    原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...

随机推荐

  1. 解决Win8无法升级.NET Framework 3.5.1 提示错误0x800F0906

    搞了好久,发现了这篇文,很清晰,就引用了过来.http://www.xdowns.com/article/239/Article_3065.html 起因是windows8.1装oracle10g提示 ...

  2. 未能添加对***.dll的引用问题

    在机器A上运行.编译成功的程序,在B上不能正确被编译,并且添加r如 knbook.dll 时候提示如下: 未能添加对"knbook.dll"引用,请确保此文件可以访问并且是一个有效 ...

  3. [机器学习] 虚拟机VMware中使用Ubuntu的联网问题

    在VMware中安装Ubuntu要解决两个问题: 1.VMware Tools安装使用 2.Ubuntu联网的虚拟机设置 1.VMware Tools安装 它的作用就是使用户可以从物理主机直接往虚拟机 ...

  4. css 选择器优先级

    优先级自上而下逐渐递减 1. 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. 2.作为style属性写在元素内的样式 3.id选择器 4.类选择器 5.标签选择器 6.通配 ...

  5. 什么是Ajax?

    1. 什么是Ajax?Asynchronous JavaScript and XML(异步的Javascript和XML),它不是某种编程语言,而是一种在无需刷新整个网页的情况下能够更新部分网页的技术 ...

  6. python 之sqlalchemy many to one

    通过查询多个父亲,对应一个儿子 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: zengchuny ...

  7. Cenos7 编译安装 Mariadb Nginx PHP Memcache ZendOpcache (实测 笔记 Centos 7.0 + Mariadb 10.0.15 + Nginx 1.6.2 + PHP 5.5.19)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...

  8. CSS实现元素水平/垂直居中的方法

    首先,我们来了解水平居中,它有很多种方法,我们暂时先来了解其中的几种: 1.    在实现方案中,我们最熟悉的莫过于给元素定义一个宽度,然后使用margin: 1 2 3 4 body{     wi ...

  9. 数据分析之pandas入门

    一.数据结构 1. Series 1.1 序列构造和调用 Series是一种类似于一维数组的对象,它由一组数据和索引共同组成,可以通过索引的方式来选取Series中的单个或一组值,常用的构造函数为ob ...

  10. 给备战NOIP 2014 的战友们的10条建议

    应老胡要求,要写10条建议= = begin 1. 注意文件关联 比如 halt 前要close(input); close(output); 还有就是一定要打这两句话= = 2. 快排,大家都懂得. ...