zoukankan      html  css  js  c++  java
  • 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();会报错,只能遍历赋值了

  • 相关阅读:
    UVa-10317
    UVa-1595
    UVa-10391
    UVa-10763
    UVa-10935
    UVa-1594
    UVa-1593
    从CSDN搬过来了
    memset会显著增加时间和空间的消耗吗
    memset对数组的初始化
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/6097000.html
Copyright © 2011-2022 走看看