You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
intcal(intn) {
intret = 0;
inti = 1;
for (; i < n; ++i) {
ret = ret + f(i);
}
// n
}
intf(intn) {
intsum = 0;
inti = 1;
for (; i < n; ++i) {
sum = sum + i;
}
// returnsum;
}
// 最终为 O(n*n)
事后统计法
通过监控、统计把代码跑一遍的方法。
大 O 复杂度表示法
T(n)表示代码的执行时间,f(n)表示每行代码的执行次数总和,O代表一个函数,表示f(n)和T(n)之间关系成正比。
加法法则:总复杂度等于量级最大的那段代码的复杂度
乘法法则:嵌套代码的复杂度等于嵌套内外代码复杂度的乘积
多个数据规模情况
计算出各个数据的复杂度并遵循加法、乘法法则。
复杂度量级
最好、坏情况时间复杂度
变量x可以出现在数组的任意位置: 出现在第一个,复杂度为O(1)[最好情况];出现在最后一个,复杂度为O(n)[最坏情况]。
平均情况时间复杂度
将概率加入到计算中,计算查找每个值的概率
均摊时间复杂度
均摊时间复杂度就是一种特殊的平均时间复杂度
The text was updated successfully, but these errors were encountered: