力扣-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:用于指定要插入的元素。
作者:Ryanjie
出处:http://www.cnblogs.com/ryanjan/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [推荐],谢谢!