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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);

View File

@ -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);

View File

@ -131,13 +131,57 @@ comments: true
=== "JavaScript"
```js title=""
/* 类 */
class Node {
val;
next;
constructor(val) {
this.val = val === undefined ? 0 : val; // 结点值
this.next = null; // 指向下一结点的引用
}
}
/* 函数 */
function constFunc() {
// do something
return 0;
}
function algorithm(n) { // 输入数据
const a = 0; // 暂存数据(常量)
const b = 0; // 暂存数据(变量)
const node = new Node(0); // 暂存数据(对象)
const c = constFunc(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
=== "TypeScript"
```typescript title=""
/* 类 */
class Node {
val: number;
next: Node | null;
constructor(val?: number) {
this.val = val === undefined ? 0 : val; // 结点值
this.next = null; // 指向下一结点的引用
}
}
/* 函数 */
function constFunc(): number {
// do something
return 0;
}
function algorithm(n: number): number { // 输入数据
const a = 0; // 暂存数据(常量)
const b = 0; // 暂存数据(变量)
const node = new Node(0); // 暂存数据(对象)
const c = constFunc(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
=== "C"
@ -266,13 +310,25 @@ comments: true
=== "JavaScript"
```js title=""
function algorithm(n) {
const a = 0; // O(1)
const b = new Array(10000); // O(1)
if (n > 10) {
const nums = new Array(n); // O(n)
}
}
```
=== "TypeScript"
```typescript title=""
function algorithm(n: number): void {
const a = 0; // O(1)
const b = new Array(10000); // O(1)
if (n > 10) {
const nums = new Array(n); // O(n)
}
}
```
=== "C"
@ -400,13 +456,41 @@ comments: true
=== "JavaScript"
```js title=""
function constFunc() {
// do something
return 0;
}
/* 循环 O(1) */
function loop(n) {
for (let i = 0; i < n; i++) {
constFunc();
}
}
/* 递归 O(n) */
function recur(n) {
if (n === 1) return;
return recur(n - 1);
}
```
=== "TypeScript"
```typescript title=""
function constFunc(): number {
// do something
return 0;
}
/* 循环 O(1) */
function loop(n: number): void {
for (let i = 0; i < n; i++) {
constFunc();
}
}
/* 递归 O(n) */
function recur(n: number): void {
if (n === 1) return;
return recur(n - 1);
}
```
=== "C"
@ -580,13 +664,43 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 常数阶 */
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();
}
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 常数阶 */
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();
}
}
```
=== "C"
@ -748,13 +862,41 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 线性阶 */
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());
}
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 线性阶 */
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());
}
}
```
=== "C"
@ -877,13 +1019,23 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 线性阶(递归实现) */
function linearRecur(n) {
console.log(`递归 n = ${n}`);
if (n === 1) return;
linearRecur(n - 1);
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 线性阶(递归实现) */
function linearRecur(n: number): void {
console.log(`递归 n = ${n}`);
if (n === 1) return;
linearRecur(n - 1);
}
```
=== "C"
@ -997,13 +1149,39 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 平方阶 */
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);
}
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 平方阶 */
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);
}
}
```
=== "C"
@ -1032,7 +1210,6 @@ $$
numList.Add(tmp);
}
}
```
=== "Swift"
@ -1120,13 +1297,25 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 平方阶(递归实现) */
function quadraticRecur(n) {
if (n <= 0) return 0;
const nums = new Array(n);
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
return quadraticRecur(n - 1);
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 平方阶(递归实现) */
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);
}
```
=== "C"
@ -1146,7 +1335,6 @@ $$
int[] nums = new int[n];
return quadraticRecur(n - 1);
}
```
=== "Swift"
@ -1239,13 +1427,27 @@ $$
=== "JavaScript"
```js title="space_complexity.js"
/* 指数阶(建立满二叉树) */
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;
}
```
=== "TypeScript"
```typescript title="space_complexity.ts"
/* 指数阶(建立满二叉树) */
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;
}
```
=== "C"