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

View File

@ -0,0 +1,101 @@
/**
* File: space_complexity.js
* Created Time: 2023-02-05
* Author: Justin (xiefahit@gmail.com)
*/
const { ListNode } = require('../include/ListNode');
const { TreeNode } = require('../include/TreeNode');
const { printTree } = require('../include/PrintUtil');
/* 函数 */
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);

View File

@ -30,7 +30,7 @@ const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 元素value: 索引
}
const index = hashingSearch(map, target);
const index = hashingSearchArray(map, target);
console.log("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
@ -41,5 +41,5 @@ while (head != null) {
map1.set(head.val, head); // key: 结点值value: 结点
head = head.next;
}
const node = hashingSearch1(map1, target);
const node = hashingSearchLinkedList(map1, target);
console.log("目标结点值 3 的对应结点对象为", node);