Add JavaScript and TypeScript code and docs for Section Space Complexity (#331)

* Fix bug before commit 5eae708

* Update queue.md

* Update the coding style for JavaScript

* Add JavaScript and TypeScript code for Section Space Complexity

* Add JavaScript and TypeScript code to docs for Section Space Complexity

* Update hashing_search.js

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
Justin Tse
2023-02-06 01:24:22 +08:00
committed by GitHub
parent 063501068b
commit bc88e52955
4 changed files with 424 additions and 20 deletions
@@ -0,0 +1,101 @@
/**
* File: space_complexity.ts
* Created Time: 2023-02-05
* Author: Justin (xiefahit@gmail.com)
*/
import { ListNode } from '../module/ListNode';
import { TreeNode } from '../module/TreeNode';
import { printTree } from '../module/PrintUtil';
/* 函数 */
function constFunc(): number {
// do something
return 0;
}
/* 常数阶 */
function constant(n: number): void {
// 常量、变量、对象占用 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: number): void {
// 长度为 n 的数组占用 O(n) 空间
const nums = new Array(n);
// 长度为 n 的列表占用 O(n) 空间
const nodes: ListNode[] = [];
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: number): void {
console.log(`递归 n = ${n}`);
if (n === 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
function quadratic(n: number): void {
// 矩阵占用 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: number): number {
if (n <= 0) return 0;
const nums = new Array(n);
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
return quadraticRecur(n - 1);
}
/* 指数阶(建立满二叉树) */
function buildTree(n: number): TreeNode | null {
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);