LeetCode: Queue Reconstruction by Height
这题的关键点在于对数组的重排序方法,高度先由高到低排列不会影响第二个参数,因为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的更多相关文章
- [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 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- 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 ...
- [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 ...
- 【LeetCode】406. Queue Reconstruction by Height 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 406. Queue Reconstruction by Height
https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...
- [leetcode] 406. Queue Reconstruction by Height (medium)
原题 思路: 一开始完全没有思路..看了别人的思路才解出来. 先按照他们的高度从高到低(因为我后面用的从前往后遍历插入,当然也可以从低到高)排序,如果高度一样,那么按照k值从小到大排序. 排完序后我们 ...
随机推荐
- php 小函数
1 变量函数 a.is_xxx函数用来判断变量类型 is_numeric (PHP 4, PHP 5) — 检测变量是否为数字或数字字符串 is_int.is_integer.is_long,判断变 ...
- uploadify前台上传文件,java后台处理的例子
1.先创建一个简单的web项目upload (如图1-1) 2.插件的准备 (1).去uploadify的官网下载一个uploadify插件,然后解压新建个js文件夹放进去(这个不强求,只要路径对了就 ...
- 我的jQuery源码读后感
(function(window, undefined) { // 构造jQuery对象 var jQuery = (function() { var jQuery = function(select ...
- js 无缝滚动效果学习
<!DOCTYPE html> <html> <head> <title>无缝滚动测试</title> <meta http-equi ...
- 全新的博客之旅&大学生活
博客之旅: 刚刚申请了博客,感觉非常兴奋,整个人都变得有精神了. 想来几个月之前看到奇奇申了博客,在上面写文章,写各种解题报告,心里就好羡慕,好希望将来有一天,也能有一个属于自己的博客.由于之前课业压 ...
- 利用JS实现购物网站商品放大镜效果
大家几乎都有在网上购物的经验,有的网站会有一个商品放大镜功能, 就是把鼠标移到图片上的时候,旁边会有另外一张大的图片展示,等同于 放大镜效果,那这样的效果怎样实现的呢,我把代码发给大家,请大家参考. ...
- 使用Hibernate SQLQuery(转)
原文地址:http://itindex.net/detail/51776-hibernate-sqlquery-sql,重新排了一下版 Hibernate对原生SQL查询的支持和控制是通过SQLQue ...
- IOS网络第五天 AFN-01发送get和post请求
************AFN01-基本使用 #import "HMViewController.h" #import "AFNetworking.h" @in ...
- 用3D再现2D偶像的可爱,Cygames【偶像大师 灰姑娘女孩】开发示例
作为万代南梦宫娱乐的偶像养成游戏[偶像大师]的派生作品,是由Mobage创作的[偶像大师 灰姑娘女孩].这个游戏的最新作品,是现在累计下载突破1200万的大作,Android/iOS平台的 ...
- 【偶像大师 白金星光】的【Variable Tone】技术大公开!偶像从哪里看都那么可爱,VA小组谈制作方针
http://game.watch.impress.co.jp/docs/news/1016369.html 自从街机版的运营依赖,今年迎来了[偶像大师]系列的11周年.在CEDEC ...