image.png

时间复杂度

O(1)

  1. const n = 1000
  2. console.log(n)

O(n)

  1. for (let i = 1; i <= 10; i++) {
  2. console.log(i)
  3. }

O(n²)

  1. for (let i = 1; i <= 10; i++) {
  2. for(let j = i + 1; j <= 10; j++){
  3. console.log(i,j)
  4. }
  5. }

O(log(n))

  1. for (let i = 1; i < n ; i = i * 2) {
  2. console.log(i)
  3. }

O(k^n)

  1. function fib(n) {
  2. if(n < 2) return n
  3. return fib(n-1) + fib(n-2)
  4. }

image.png