public static long Compute(int x){
long count = 0;
if(x==1)
return count=1;
else if(x==3)
for(int i =0;i<x;i++){
count+=i;
}
else if (x==5)
for(int j=0;j<x;j++)
for(int k=0;j<=x;k++)
return +=(i*j);
}
}
return result;
}
我想了解如何解决每一个语句来制定运行时的T(n)。我有2个if语句和一个for-loop和一个嵌套for-loop。我的答案是O(n^2)。
f = O(g)意味着有一个N和c,所以n>N意味着f(n) <= c * g(n)。我假设f和g是非负数)。
该代码需要x>5的恒定时间。所以是O(1)。