为什么这段代码的运行时间是O(n + n)?

问题描述 投票:0回答:0

我明白 x 行是 O(n),但是 y + z 行应该是 O(1),不是吗?所以总运行时间为 O(n + 1) = O(n)。但正确答案是 O(n + n) = O(n)。这是为什么?

foo2(int n){
    for(int i=1 to n) //line x
        if(i==y^4){.  //line y
            for(j=1 to n)  //line z
                { print "hi there" } 
        }//endif
        print "hello";
    }//endfor
}//foo()
time time-complexity runtime big-o
© www.soinside.com 2019 - 2024. All rights reserved.