力扣-406-根据身高重建队伍

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        int len = people.length;
		List<int[]> ans = new ArrayList<int[]>();
		
		Arrays.sort(people, new Comparator<int[]>(){
			@Override
			public int compare(int[] o1, int[] o2) {
				//o1 - o2 是从小到大排序;o2 - o1是从大到小排序(默认是)
				return o1[0] == o2[0]? o1[1] - o2[1]: o2[0] - o1[0];
			}
		});
		
		for(int[] p: people) {
			ans.add(p[1], p);
		}
		return ans.toArray(new int[len][2]);
    }
}

 

这里用到了List.add操作,add(E e) 用于在列表的尾部插入指定元素。如果 List 集合对象由于调用 add 方法而发生更改,则返回 true;否则返回 false。

void add(int index,E element) 用于在列表的指定位置插入指定元素,并将当前处于该位置的元素及其后续元素的索引加 1。

参数说明:

  • index:用于指定在其中插入指定元素处的索引。
  • element:用于指定要插入的元素。
posted @ 2020-10-16 16:23  Peterxiazhen  阅读(167)  评论(0编辑  收藏  举报