algorithm - 计算算法复杂度 - 困惑

标签 algorithm language-agnostic time-complexity

我有以下代码片段:

sum = 0;
for (i = 0; i < n; i++)
    for (j = 0; j < i; j++)
        sum++;

复杂度为 O(n^2),但如果我想深入了解内部循环的复杂度,那么它会是 (n (n-1))/2(n-1)!?

最佳答案

是的,O(n^2),但实际上 0+1+...+n-1=n(n-1)/2 = O(n^2),绝对不是 (n-1)!

关于algorithm - 计算算法复杂度 - 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2233938/

相关文章:

在 GPS 坐标数据库中查找 'hot spots' 的算法

algorithm - 划分三次样条计算

c - 向后递增数组中的值

time-complexity - 时间复杂度: O(log n) versus O(log 2n)

performance - 遗传算法的时间复杂度

algorithm - 渐近。如果 f(n) = theta(g(n)) 且 g(n) = theta(h(n)),那么为什么 h(n) = theta(f(n))

algorithm - 龟兔赛跑算法对一种情况返回空结果,对重复字符不正确

ruby - "Pyramidizing"一个数组/列表(在 Ruby 中,但可能可以实现通用解决方案)

language-agnostic - 支持 "partial specialization"的其他语言有哪些?

Java:为什么获取数组的长度 O(1)?