当有两个单独的for循环时,如何找到Big O.

dav*_*234 2 c big-o for-loop

我知道如何为"for循环"和嵌套的"for循环"找到Big O,但是当我们有两个for循环,而不是嵌套但在同一个函数中有两个独立的for循环时会发生什么.

cru*_*fux 7

它只是被添加.

参见例如:

for(i=0;i<n;i++)
    //statements

for(i=0;i<m;i++)
    //statements
Run Code Online (Sandbox Code Playgroud)

因此总复杂度为O(m + n).

比方说m = 3n然后是O(4n),它只是O(n).

设m = n ^ 2

那么它的O(n ^ 2 + n)是O(n ^ 2)