简体   繁体   English

确定时间复杂度

[英]Determine the time complexity

Can someone help determine the time complexity of this code.有人可以帮助确定此代码的时间复杂度。 Is this just O(1)?这只是 O(1) 吗? If it is not can someone help explain it?如果不是,有人可以帮忙解释一下吗? I'm not 100% sure about my answer so I need a second opinion to see if this function is O(1).我不是 100% 确定我的答案,所以我需要第二个意见,看看这个 function 是否为 O(1)。

    public static void secMax(int[] arr)
    {
        int n = arr.length - 1;
       

        if (some condition not relate to n)
        {
           
            if(arr[a] * arr[b] > temp)
            {
                //print
            }
            else
            {
                //do something
            }
        }
        else
        {
            if(arr[y] * arr[x] > second)
            {
                //something
            }
            else
            {
                //something else
            }
        }
        
    }

It is O(1).它是 O(1)。 You don't have any loop that traverses through the array.您没有任何遍历数组的循环。 And you don't have any recursive call.而且你没有任何递归调用。 Your method only have few if/else conditions.您的方法只有很少的 if/else 条件。 If you check carefully, your method only do fixed number of operations & that doesn't vary depending on the input array length.如果您仔细检查,您的方法只会执行固定数量的操作,并且不会因输入数组长度而异。 So time complexity is constant.所以时间复杂度是恒定的。

It is O(1).它是 O(1)。 The program just simply flows from top to bottom through a few conditional statements.该程序只是简单地通过几个条件语句从上到下流动。 No block of code executes more than once, so it has constant complexity.没有代码块执行超过一次,因此它具有恒定的复杂性。

It's O(1) in time complexity, since you don't do any loops to traverse your argument array, neither recursive calls时间复杂度为 O(1),因为您不执行任何循环来遍历参数数组,也不执行递归调用

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

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