• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Queue Reconstruction by Height

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

 1 public class Solution {
 2     public int[][] reconstructQueue(int[][] people) {
 3         Arrays.sort(people, new Comparator<int[]>() {
 4             public int compare(int[] o1, int[] o2) {
 5                 return o1[0] == o2[0]? o1[1] - o2[1] : o2[0] - o1[0];
 6             }
 7         });
 8         List<int[]> pList = new ArrayList<int[]>();
 9         for (int i = 0; i < people.length; i++) {
10             pList.add(people[i][1], new int[]{people[i][0], people[i][1]});
11         }
12         int[][] ans = new int[people.length][2];
13         for (int i = 0; i < pList.size(); i++) {
14             ans[i] = pList.get(i);
15         }
16         return ans;
17     }
18 }

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

posted @ 2016-11-24 11:19  ying_vincent  阅读(185)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3