gpt4 book ai didi

java - 这个程序的运行时复杂度是多少?(尤其是 retainAll() 方法)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:02 40 4
gpt4 key购买 nike

public static List<Integer> returnIntersection(List<Integer> a,List<Integer> b){

List<Integer> l1=new ArrayList<Integer>(a);
List<Integer> l2=new ArrayList<Integer>(b);
l1.retainAll(l2);//find intersection in l2
l1=removeDuplicates(l1);
return l1;}

public static List<Integer> removeDuplicates(List<Integer> l) {

Set<Integer> se=new HashSet<Integer>(l);
l.clear();
l=new ArrayList<Integer>(se);
return l;}

上面的代码是返回一个列表,其中包含 2 个列表的交集,没有重复项。我的问题是这个的时间复杂度是多少? retainAll() 方法的时间复杂度是多少?列表转集合是否耗时?

最佳答案

一个有趣的话题是衡量单个方法的复杂性。有几个因素导致了复杂性。

看这里how to measure complexity

如何计算复杂度是个很好的网站

关于java - 这个程序的运行时复杂度是多少?(尤其是 retainAll() 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15491934/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com