【问题标题】:What is the runtime complexity of this program?(esp retainAll() method)这个程序的运行时复杂度是多少?(尤其是 retainAll() 方法)
【发布时间】:2013-03-19 05:14:22
【问题描述】:
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() 方法的时间复杂度是多少?将列表转换为集合是否有任何耗时?

【问题讨论】:

  • 你看过实现了吗?

标签: java list set


【解决方案1】:

一个有趣的话题是衡量单个方法的复杂性。有几件事会导致复杂性。

看这里how to measure complexity

如何计算复杂度是非常好的网站

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 2019-06-08
    • 2021-06-14
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多