简体   繁体   English

Big-O运行时,以确定List是否包含X和F(X)

[英]Big-O runtime to determine if List contains X and F(X)

I am trying to understand what the Big O would be for the following method. 我正在尝试了解以下方法的大目标。

    for(Integer i : list){
        if(list.contains(-i))
            //doSomething
    }

I know that the contains method is O(n) and that a for loop like that is O(n) as well. 我知道contains方法是O(n),像这样的for循环也是O(n)。 Does that make the total for this method O(n * n)? 这是否使该方法的总数为O(n * n)?

假设List.contains是O(n),那么是的,整个算法是O(n ^ 2)。

BUt if you use an iterator on your list, and use the hasNext method, then your total worst case running time will big O(n). 但是,如果在列表上使用迭代器,并使用hasNext方法,则最坏情况下的总运行时间将为O(n)。 Aprroximate formula will be a constant plus n, where n is the if condition. 近似公式将是常数加n,其中n是if条件。 the iterator takes a constant time to return an object, unlike the for loop. 与for循环不同,迭代器花费固定的时间返回对象。

因此,假设m和n是两个列表,则总运行时间将为n + 1m + 2m + ... +(n-n + 1)m。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM