BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #52797同步于 2016/9/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

List中元素顺序问题

linsiy
2016/9/8镜像同步9 回复
leetcode遇到一个题,要求输出一个数组中数的全部排序,返回List<List<Integer>>。 题目中给出的例子是: 我以为就是要按照字典顺序输出,但是看到disscuss中没有考虑字典顺序,而是随便输出的。代码如下 public List<List<Integer>> permute(int[] num) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); if (num.length ==0) return ans; List<Integer> l0 = new ArrayList<Integer>(); l0.add(num[0]); ans.add(l0); for (int i = 1; i< num.length; ++i){ List<List<Integer>> new_ans = new ArrayList<List<Integer>>(); for (int j = 0; j<=i; ++j){ for (List<Integer> l : ans){ List<Integer> new_l = new ArrayList<Integer>(l); new_l.add(j,num[i]); new_ans.add(new_l); } } ans = new_ans; } return ans; } 我将倒数第七行new_ans.add(new_l)改为new_ans.add(0,new_l),结果仍然是AC。两次顺序完全不同的List都AC了,List不是有序的吗,求助~
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
icyfox机器人#1 · 2016/9/8
那不就是判卷子的认为顺序不对也无所谓么
hbxtght机器人#2 · 2016/9/8
顺序无所谓而已....
dss886机器人#3 · 2016/9/8
我记得题目里说了顺序无所谓的吧。。。
maoxian机器人#4 · 2016/9/8
list的有序是按照元素的插入顺序,而不是调用sort方法后的那个排序
NachtZ机器人#5 · 2016/9/9
这题我记得,不要求顺序吧。
linsiy机器人#6 · 2016/9/9
好吧,题中倒是没有题要求有序,看来只是因为本题顺序不对也无所谓,谢谢大家[ema3]
jh1机器人#7 · 2016/9/9
还换头像了。 【 在 NachtZ 的大作中提到: 】 : 这题我记得,不要求顺序吧。
napoleonwxu机器人#8 · 2016/9/9
二楼正解
NachtZ机器人#9 · 2016/9/9
换了个更好看的喵。 【 在 jh1 的大作中提到: 】 : 还换头像了。