2023-02-06 01:24:22 +08:00
|
|
|
/**
|
|
|
|
* File: space_complexity.js
|
|
|
|
* Created Time: 2023-02-05
|
|
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-03-03 01:34:53 +08:00
|
|
|
const { ListNode } = require('../modules/ListNode');
|
|
|
|
const { TreeNode } = require('../modules/TreeNode');
|
|
|
|
const { printTree } = require('../modules/PrintUtil');
|
2023-02-06 01:24:22 +08:00
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
function constFunc() {
|
|
|
|
// do something
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 常数阶 */
|
|
|
|
function constant(n) {
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
const a = 0;
|
|
|
|
const b = 0;
|
|
|
|
const nums = new Array(10000);
|
|
|
|
const node = new ListNode(0);
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
const c = 0;
|
|
|
|
}
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
constFunc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶 */
|
|
|
|
function linear(n) {
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
const nums = new Array(n);
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
const nodes = [];
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
nodes.push(new ListNode(i));
|
|
|
|
}
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
const map = new Map();
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
map.set(i, i.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
function linearRecur(n) {
|
|
|
|
console.log(`递归 n = ${n}`);
|
|
|
|
if (n === 1) return;
|
|
|
|
linearRecur(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶 */
|
|
|
|
function quadratic(n) {
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
const numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
const numList = [];
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
const tmp = [];
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
tmp.push(0);
|
|
|
|
}
|
|
|
|
numList.push(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
function quadraticRecur(n) {
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
const nums = new Array(n);
|
|
|
|
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
function buildTree(n) {
|
|
|
|
if (n === 0) return null;
|
|
|
|
const root = new TreeNode(0);
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
const n = 5;
|
|
|
|
// 常数阶
|
|
|
|
constant(n);
|
|
|
|
// 线性阶
|
|
|
|
linear(n);
|
|
|
|
linearRecur(n);
|
|
|
|
// 平方阶
|
|
|
|
quadratic(n);
|
|
|
|
quadraticRecur(n);
|
|
|
|
// 指数阶
|
|
|
|
const root = buildTree(n);
|
|
|
|
printTree(root);
|