From eb93939f689d7323a4ee30c49d7ef48f298acc0c Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 12:02:21 +1100 Subject: [PATCH 01/13] added: typescript hash func --- .../chapter_hashing/array_hash_map.ts | 137 ++++++++++++++++++ codes/typescript/chapter_hashing/hash_map.ts | 45 ++++++ codes/typescript/tsconfig.json | 16 ++ docs/chapter_hashing/hash_map.md | 110 +++++++++++++- hello-algo.iml | 11 ++ 5 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 codes/typescript/chapter_hashing/array_hash_map.ts create mode 100644 codes/typescript/chapter_hashing/hash_map.ts create mode 100644 codes/typescript/tsconfig.json create mode 100644 hello-algo.iml diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts new file mode 100644 index 000000000..d56fb8fd7 --- /dev/null +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -0,0 +1,137 @@ +/* 键值对 int->String */ +class Entry { + public key: number; + public val: string; + constructor(key: number, val: string) { + this.key = key; + this.val = val; + } +} + +/* 数组的初始化和基本操作 */ +class ArrayList { + + private readonly elements: Entry[]; + constructor(length: number) { + this.elements = new Array(length); + this.initialize(); + } + + /* 初始化 */ + private initialize() { + this.elements.fill(null as any, 0, this.elements.length - 1); + } + + /* 新增和删除 */ + public set(key: number, val: string | null) { + this.isOutOfRange(key); + if (val !== null) { + this.elements[key] = new Entry(key, val); + } + this.elements[key] = null as any; + } + + /* 获取 */ + public get(key: number): string { + return this.elements[key].val; + } + + public entrySet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i]); + } + } + return arr; + } + + public valueSet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i].val); + } + } + return arr; + } + + public keySet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i].key); + } + } + return arr; + } + + private isOutOfRange(key: number) { + if (key > this.elements.length - 1) { + throw new Error('Out of array range'); + } + } +} + +/* 基于数组简易实现的哈希表 */ +class ArrayHashMap { + // 初始化一个长度为 100 的桶(数组) + private bucket: ArrayList; + + constructor() { + this.bucket = new ArrayList(100); + } + + /* 哈希函数 */ + private hashFunc(key: number): number { + return key % 100; + } + + /* 查询操作 */ + public get(key: number): string | null { + let index = this.hashFunc(key); + let val = this.bucket.get(index); + if (val === null) { + return null; + } + return val; + } + + /* 添加操作 */ + public put(key: number, val: string) { + let index = this.hashFunc(key); + this.bucket.set(index, val); + } + + /* 删除操作 */ + public remove(key: number) { + let index = this.hashFunc(key); + // 置为 null ,代表删除 + this.bucket.set(index, null); + } + + /* 获取所有键值对 */ + public entrySet(): Entry[] { + return this.bucket.entrySet(); + } + + /* 获取所有键 */ + public keySet(): number[] { + return this.bucket.keySet(); + } + + /* 获取所有值 */ + public valueSet(): string[] { + return this.bucket.valueSet(); + } + +/* 打印哈希表 */ + public print() { + let entrySet = this.entrySet(); + for (const entry of entrySet) { + console.info(`${entry.key} -> ${entry.val}`); + } + } +} + +export default ArrayHashMap; diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts new file mode 100644 index 000000000..8849d98f3 --- /dev/null +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -0,0 +1,45 @@ +import ArrayHashMap from './array_hash_map'; + +class HashMap { + + constructor() { + /* 初始化哈希表 */ + const map = new ArrayHashMap(); + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, '小哈'); + map.put(15937, '小啰'); + map.put(16750, '小算'); + map.put(13276, '小法'); + map.put(10583, '小鸭'); + console.info('\n添加完成后,哈希表为\nKey -> Value'); + map.print(); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + let name = map.get(15937); + console.info('\n输入学号 15937 ,查询到姓名 ' + name); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583); + console.info('\n删除 10583 后,哈希表为\nKey -> Value'); + map.print(); + + /* 遍历哈希表 */ + console.info('\n遍历键值对 Key->Value'); + for (const entry of map.entrySet()) { + console.info(entry.key + ' -> ' + entry.val); + } + console.info('\n单独遍历键 Key'); + for (const key of map.keySet()) { + console.info(key); + } + console.info('\n单独遍历值 Value'); + for (const val of map.valueSet()) { + console.info(val); + } + } +} + +export default HashMap; \ No newline at end of file diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json new file mode 100644 index 000000000..c53bf4f72 --- /dev/null +++ b/codes/typescript/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es6", + "moduleResolution": "node", + "resolveJsonModule": true, + "module": "esnext", + "strict": true, + "importHelpers": true, + "noEmit": false, + "sourceMap": true, + "baseUrl": ".", + "esModuleInterop": true, + "downlevelIteration": true, + }, + "include": ["./**/*"], +} diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 3a53f5dba..091e1242f 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -138,7 +138,28 @@ comments: true === "TypeScript" ```typescript title="hash_map.ts" + /* 初始化哈希表 */ + const map = new ArrayHashMap(); + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, '小哈'); + map.put(15937, '小啰'); + map.put(16750, '小算'); + map.put(13276, '小法'); + map.put(10583, '小鸭'); + console.info('\n添加完成后,哈希表为\nKey -> Value'); + map.print(); + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + let name = map.get(15937); + console.info('\n输入学号 15937 ,查询到姓名 ' + name); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583); + console.info('\n删除 10583 后,哈希表为\nKey -> Value'); + map.print(); ``` === "C" @@ -233,7 +254,19 @@ comments: true === "TypeScript" ```typescript title="hash_map.ts" - + /* 遍历哈希表 */ + console.info('\n遍历键值对 Key->Value'); + for (const entry of map.entrySet()) { + console.info(entry.key + ' -> ' + entry.val); + } + console.info('\n单独遍历键 Key'); + for (const key of map.keySet()) { + console.info(key); + } + console.info('\n单独遍历值 Value'); + for (const val of map.valueSet()) { + console.info(val); + } ``` === "C" @@ -477,7 +510,80 @@ $$ === "TypeScript" ```typescript title="array_hash_map.ts" - + /* 键值对 int->String */ + class Entry { + public key: number; + public val: string; + constructor(key: number, val: string) { + this.key = key; + this.val = val; + } + } + + /* 数组的初始化和基本操作 */ + class ArrayList { + + private readonly elements: Entry[]; + constructor(length: number) { + this.elements = new Array(length); + this.initialize(); + } + + /* 初始化 */ + private initialize() { + this.elements.fill(null as any, 0, this.elements.length - 1); + } + + /* 新增和删除 */ + public set(key: number, val: string | null) { + this.isOutOfRange(key); + if (val !== null) { + this.elements[key] = new Entry(key, val); + } + this.elements[key] = null as any; + } + + /* 获取 */ + public get(key: number): string { + return this.elements[key].val; + } + + public entrySet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i]); + } + } + return arr; + } + + public valueSet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i].val); + } + } + return arr; + } + + public keySet() { + let arr = []; + for (let i = 0; i < this.elements.length; i++) { + if (this.elements[i] !== null) { + arr.push(this.elements[i].key); + } + } + return arr; + } + + private isOutOfRange(key: number) { + if (key > this.elements.length - 1) { + throw new Error('Out of array range'); + } + } + } ``` === "C" diff --git a/hello-algo.iml b/hello-algo.iml new file mode 100644 index 000000000..0a762e4af --- /dev/null +++ b/hello-algo.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 1ef6cd09872576d9ea2bd4471fc306ecf08251a1 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 12:12:46 +1100 Subject: [PATCH 02/13] updated: .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 70b2f49fe..3ff305496 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ docs/overrides/ # python files __pycache__ + +# iml +hello-algo.iml From 6eec01d594c6be44f1d0d7d57402bdf2fd1b94ad Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 12:27:44 +1100 Subject: [PATCH 03/13] removed: out of array range check --- codes/typescript/chapter_hashing/array_hash_map.ts | 7 ------- docs/chapter_hashing/hash_map.md | 7 ------- 2 files changed, 14 deletions(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index d56fb8fd7..d5b5a9f80 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -24,7 +24,6 @@ class ArrayList { /* 新增和删除 */ public set(key: number, val: string | null) { - this.isOutOfRange(key); if (val !== null) { this.elements[key] = new Entry(key, val); } @@ -65,12 +64,6 @@ class ArrayList { } return arr; } - - private isOutOfRange(key: number) { - if (key > this.elements.length - 1) { - throw new Error('Out of array range'); - } - } } /* 基于数组简易实现的哈希表 */ diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 091e1242f..2fed2facd 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -536,7 +536,6 @@ $$ /* 新增和删除 */ public set(key: number, val: string | null) { - this.isOutOfRange(key); if (val !== null) { this.elements[key] = new Entry(key, val); } @@ -577,12 +576,6 @@ $$ } return arr; } - - private isOutOfRange(key: number) { - if (key > this.elements.length - 1) { - throw new Error('Out of array range'); - } - } } ``` From bd21fd8be9e8e2a07f41658992ec012729d34495 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 19:29:06 +1100 Subject: [PATCH 04/13] fixed: several bugs --- .../chapter_hashing/array_hash_map.ts | 23 +++--- codes/typescript/tsconfig.json | 3 +- docs/chapter_hashing/hash_map.md | 71 ++++++++++++++++--- 3 files changed, 77 insertions(+), 20 deletions(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index d5b5a9f80..e4d1c83f5 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -1,4 +1,4 @@ -/* 键值对 int->String */ +/* 键值对 Number -> String */ class Entry { public key: number; public val: string; @@ -26,19 +26,23 @@ class ArrayList { public set(key: number, val: string | null) { if (val !== null) { this.elements[key] = new Entry(key, val); + } else { + this.elements[key] = null as any; } - this.elements[key] = null as any; } /* 获取 */ - public get(key: number): string { - return this.elements[key].val; + public get(key: number): string | null { + if (this.elements[key] instanceof Entry) { + return this.elements[key].val; + } + return null; } public entrySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i]); } } @@ -48,7 +52,7 @@ class ArrayList { public valueSet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].val); } } @@ -58,7 +62,7 @@ class ArrayList { public keySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].key); } } @@ -84,9 +88,7 @@ class ArrayHashMap { public get(key: number): string | null { let index = this.hashFunc(key); let val = this.bucket.get(index); - if (val === null) { - return null; - } + if (val === null) return null; return val; } @@ -122,6 +124,7 @@ class ArrayHashMap { public print() { let entrySet = this.entrySet(); for (const entry of entrySet) { + if (!entry) continue; console.info(`${entry.key} -> ${entry.val}`); } } diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json index c53bf4f72..446280b19 100644 --- a/codes/typescript/tsconfig.json +++ b/codes/typescript/tsconfig.json @@ -3,9 +3,8 @@ "target": "es6", "moduleResolution": "node", "resolveJsonModule": true, - "module": "esnext", + "module": "commonjs", "strict": true, - "importHelpers": true, "noEmit": false, "sourceMap": true, "baseUrl": ".", diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 2fed2facd..981204b71 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -510,7 +510,7 @@ $$ === "TypeScript" ```typescript title="array_hash_map.ts" - /* 键值对 int->String */ + /* 键值对 Number -> String */ class Entry { public key: number; public val: string; @@ -538,19 +538,23 @@ $$ public set(key: number, val: string | null) { if (val !== null) { this.elements[key] = new Entry(key, val); + } else { + this.elements[key] = null as any; } - this.elements[key] = null as any; } /* 获取 */ - public get(key: number): string { - return this.elements[key].val; + public get(key: number): string | null { + if (this.elements[key] instanceof Entry) { + return this.elements[key].val; + } + return null; } public entrySet() { - let arr = []; + let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i]); } } @@ -560,7 +564,7 @@ $$ public valueSet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].val); } } @@ -570,13 +574,64 @@ $$ public keySet() { let arr = []; for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i] !== null) { + if (this.elements[i]) { arr.push(this.elements[i].key); } } return arr; } } + + /* 基于数组简易实现的哈希表 */ + class ArrayHashMap { + // 初始化一个长度为 100 的桶(数组) + private bucket: ArrayList; + + constructor() { + this.bucket = new ArrayList(100); + } + + /* 哈希函数 */ + private hashFunc(key: number): number { + return key % 100; + } + + /* 查询操作 */ + public get(key: number): string | null { + let index = this.hashFunc(key); + let val = this.bucket.get(index); + if (val === null) return null; + return val; + } + + /* 添加操作 */ + public put(key: number, val: string) { + let index = this.hashFunc(key); + this.bucket.set(index, val); + } + + /* 删除操作 */ + public remove(key: number) { + let index = this.hashFunc(key); + // 置为 null ,代表删除 + this.bucket.set(index, null); + } + + /* 获取所有键值对 */ + public entrySet(): Entry[] { + return this.bucket.entrySet(); + } + + /* 获取所有键 */ + public keySet(): number[] { + return this.bucket.keySet(); + } + + /* 获取所有值 */ + public valueSet(): string[] { + return this.bucket.valueSet(); + } + } ``` === "C" From 045df5847230a829a243fb31b5c5e2ef2a6f2f22 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 19:37:24 +1100 Subject: [PATCH 05/13] added: header comments --- codes/typescript/chapter_hashing/array_hash_map.ts | 6 ++++++ codes/typescript/chapter_hashing/hash_map.ts | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index e4d1c83f5..41a153d6a 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -1,3 +1,9 @@ +/* + * File: array_hash_map.ts + * Created Time: 2022-12-29 + * Author: Daniel (better.sunjian@gmail.com) + */ + /* 键值对 Number -> String */ class Entry { public key: number; diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts index 8849d98f3..a979d9fc1 100644 --- a/codes/typescript/chapter_hashing/hash_map.ts +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -1,3 +1,9 @@ +/* + * File: hash_map.ts + * Created Time: 2022-12-29 + * Author: Daniel (better.sunjian@gmail.com) + */ + import ArrayHashMap from './array_hash_map'; class HashMap { From 18b8557a6aefc834da8ef4a61ef385e5e3967747 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 19:37:39 +1100 Subject: [PATCH 06/13] added: header comments --- codes/typescript/chapter_hashing/array_hash_map.ts | 2 +- codes/typescript/chapter_hashing/hash_map.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index 41a153d6a..7ce7eeb32 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -1,6 +1,6 @@ /* * File: array_hash_map.ts - * Created Time: 2022-12-29 + * Created Time: 2022-12-20 * Author: Daniel (better.sunjian@gmail.com) */ diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts index a979d9fc1..5e37d5b01 100644 --- a/codes/typescript/chapter_hashing/hash_map.ts +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -1,6 +1,6 @@ /* * File: hash_map.ts - * Created Time: 2022-12-29 + * Created Time: 2022-12-20 * Author: Daniel (better.sunjian@gmail.com) */ From 7d0a30b1d7a1955035eb23fc7b013cfdf0514083 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 20 Dec 2022 19:47:23 +1100 Subject: [PATCH 07/13] removed: iml file --- hello-algo.iml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 hello-algo.iml diff --git a/hello-algo.iml b/hello-algo.iml deleted file mode 100644 index 0a762e4af..000000000 --- a/hello-algo.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From 1e72f66f093d5ec85427bd98cb9c4addf5705b69 Mon Sep 17 00:00:00 2001 From: danielsss Date: Thu, 22 Dec 2022 00:04:30 +1100 Subject: [PATCH 08/13] Updated code formats and removed useless codes --- .../chapter_hashing/array_hash_map.ts | 236 +++++++++--------- codes/typescript/chapter_hashing/hash_map.ts | 51 ---- codes/typescript/tsconfig.json | 15 -- docs/chapter_hashing/hash_map.md | 107 +++----- 4 files changed, 149 insertions(+), 260 deletions(-) delete mode 100644 codes/typescript/chapter_hashing/hash_map.ts delete mode 100644 codes/typescript/tsconfig.json diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index 7ce7eeb32..c1539e031 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -6,134 +6,130 @@ /* 键值对 Number -> String */ class Entry { - public key: number; - public val: string; - constructor(key: number, val: string) { - this.key = key; - this.val = val; - } -} + public key: number; + public val: string; -/* 数组的初始化和基本操作 */ -class ArrayList { - - private readonly elements: Entry[]; - constructor(length: number) { - this.elements = new Array(length); - this.initialize(); - } - - /* 初始化 */ - private initialize() { - this.elements.fill(null as any, 0, this.elements.length - 1); - } - - /* 新增和删除 */ - public set(key: number, val: string | null) { - if (val !== null) { - this.elements[key] = new Entry(key, val); - } else { - this.elements[key] = null as any; + constructor(key: number, val: string) { + this.key = key; + this.val = val; } - } - - /* 获取 */ - public get(key: number): string | null { - if (this.elements[key] instanceof Entry) { - return this.elements[key].val; - } - return null; - } - - public entrySet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i]); - } - } - return arr; - } - - public valueSet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i].val); - } - } - return arr; - } - - public keySet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i].key); - } - } - return arr; - } } /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - // 初始化一个长度为 100 的桶(数组) - private bucket: ArrayList; - constructor() { - this.bucket = new ArrayList(100); - } + private readonly bucket: Entry[]; - /* 哈希函数 */ - private hashFunc(key: number): number { - return key % 100; - } - - /* 查询操作 */ - public get(key: number): string | null { - let index = this.hashFunc(key); - let val = this.bucket.get(index); - if (val === null) return null; - return val; - } - - /* 添加操作 */ - public put(key: number, val: string) { - let index = this.hashFunc(key); - this.bucket.set(index, val); - } - - /* 删除操作 */ - public remove(key: number) { - let index = this.hashFunc(key); - // 置为 null ,代表删除 - this.bucket.set(index, null); - } - - /* 获取所有键值对 */ - public entrySet(): Entry[] { - return this.bucket.entrySet(); - } - - /* 获取所有键 */ - public keySet(): number[] { - return this.bucket.keySet(); - } - - /* 获取所有值 */ - public valueSet(): string[] { - return this.bucket.valueSet(); - } - -/* 打印哈希表 */ - public print() { - let entrySet = this.entrySet(); - for (const entry of entrySet) { - if (!entry) continue; - console.info(`${entry.key} -> ${entry.val}`); + constructor() { + // 初始化一个长度为 100 的桶(数组) + this.bucket = (new Array(100)).fill(null as any); + } + + /* 哈希函数 */ + private hashFunc(key: number): number { + return key % 100; + } + + /* 查询操作 */ + public get(key: number): string | null { + let index = this.hashFunc(key); + let entry = this.bucket[index]; + if (entry === null) return null; + return entry.val; + } + + /* 添加操作 */ + public put(key: number, val: string) { + let index = this.hashFunc(key); + this.bucket[index] = new Entry(index, val); + } + + /* 删除操作 */ + public remove(key: number) { + let index = this.hashFunc(key); + // 置为 null ,代表删除 + this.bucket[index] = null as any; + } + + /* 获取所有键值对 */ + public entrySet(): Entry[] { + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i]); + } + } + return arr; + } + + /* 获取所有键 */ + public keySet(): number[] { + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i].key); + } + } + return arr; + } + + /* 获取所有值 */ + public valueSet(): string[] { + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i].val); + } + } + return arr; + } + + /* 打印哈希表 */ + public print() { + let entrySet = this.entrySet(); + for (const entry of entrySet) { + if (!entry) continue; + console.info(`${entry.key} -> ${entry.val}`); + } } - } } -export default ArrayHashMap; + +/* 初始化哈希表 */ +const map = new ArrayHashMap(); +/* 添加操作 */ +// 在哈希表中添加键值对 (key, value) +map.put(12836, '小哈'); +map.put(15937, '小啰'); +map.put(16750, '小算'); +map.put(13276, '小法'); +map.put(10583, '小鸭'); +console.info('\n添加完成后,哈希表为\nKey -> Value'); +map.print(); + +/* 查询操作 */ +// 向哈希表输入键 key ,得到值 value +let name = map.get(15937); +console.info('\n输入学号 15937 ,查询到姓名 ' + name); + +/* 删除操作 */ +// 在哈希表中删除键值对 (key, value) +map.remove(10583); +console.info('\n删除 10583 后,哈希表为\nKey -> Value'); +map.print(); + +/* 遍历哈希表 */ +console.info('\n遍历键值对 Key->Value'); +for (const entry of map.entrySet()) { + console.info(entry.key + ' -> ' + entry.val); +} +console.info('\n单独遍历键 Key'); +for (const key of map.keySet()) { + console.info(key); +} +console.info('\n单独遍历值 Value'); +for (const val of map.valueSet()) { + console.info(val); +} + +export {}; diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts deleted file mode 100644 index 5e37d5b01..000000000 --- a/codes/typescript/chapter_hashing/hash_map.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * File: hash_map.ts - * Created Time: 2022-12-20 - * Author: Daniel (better.sunjian@gmail.com) - */ - -import ArrayHashMap from './array_hash_map'; - -class HashMap { - - constructor() { - /* 初始化哈希表 */ - const map = new ArrayHashMap(); - /* 添加操作 */ - // 在哈希表中添加键值对 (key, value) - map.put(12836, '小哈'); - map.put(15937, '小啰'); - map.put(16750, '小算'); - map.put(13276, '小法'); - map.put(10583, '小鸭'); - console.info('\n添加完成后,哈希表为\nKey -> Value'); - map.print(); - - /* 查询操作 */ - // 向哈希表输入键 key ,得到值 value - let name = map.get(15937); - console.info('\n输入学号 15937 ,查询到姓名 ' + name); - - /* 删除操作 */ - // 在哈希表中删除键值对 (key, value) - map.remove(10583); - console.info('\n删除 10583 后,哈希表为\nKey -> Value'); - map.print(); - - /* 遍历哈希表 */ - console.info('\n遍历键值对 Key->Value'); - for (const entry of map.entrySet()) { - console.info(entry.key + ' -> ' + entry.val); - } - console.info('\n单独遍历键 Key'); - for (const key of map.keySet()) { - console.info(key); - } - console.info('\n单独遍历值 Value'); - for (const val of map.valueSet()) { - console.info(val); - } - } -} - -export default HashMap; \ No newline at end of file diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json deleted file mode 100644 index 446280b19..000000000 --- a/codes/typescript/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "compilerOptions": { - "target": "es6", - "moduleResolution": "node", - "resolveJsonModule": true, - "module": "commonjs", - "strict": true, - "noEmit": false, - "sourceMap": true, - "baseUrl": ".", - "esModuleInterop": true, - "downlevelIteration": true, - }, - "include": ["./**/*"], -} diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 981204b71..896675ec6 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -512,83 +512,24 @@ $$ ```typescript title="array_hash_map.ts" /* 键值对 Number -> String */ class Entry { + public key: number; public val: string; + constructor(key: number, val: string) { this.key = key; this.val = val; } } - - /* 数组的初始化和基本操作 */ - class ArrayList { - - private readonly elements: Entry[]; - constructor(length: number) { - this.elements = new Array(length); - this.initialize(); - } - - /* 初始化 */ - private initialize() { - this.elements.fill(null as any, 0, this.elements.length - 1); - } - - /* 新增和删除 */ - public set(key: number, val: string | null) { - if (val !== null) { - this.elements[key] = new Entry(key, val); - } else { - this.elements[key] = null as any; - } - } - - /* 获取 */ - public get(key: number): string | null { - if (this.elements[key] instanceof Entry) { - return this.elements[key].val; - } - return null; - } - - public entrySet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i]); - } - } - return arr; - } - - public valueSet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i].val); - } - } - return arr; - } - - public keySet() { - let arr = []; - for (let i = 0; i < this.elements.length; i++) { - if (this.elements[i]) { - arr.push(this.elements[i].key); - } - } - return arr; - } - } /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - // 初始化一个长度为 100 的桶(数组) - private bucket: ArrayList; - + + private readonly bucket: Entry[]; + constructor() { - this.bucket = new ArrayList(100); + // 初始化一个长度为 100 的桶(数组) + this.bucket = (new Array(100)).fill(null as any); } /* 哈希函数 */ @@ -599,37 +540,55 @@ $$ /* 查询操作 */ public get(key: number): string | null { let index = this.hashFunc(key); - let val = this.bucket.get(index); - if (val === null) return null; - return val; + let entry = this.bucket[index]; + if (entry === null) return null; + return entry.val; } /* 添加操作 */ public put(key: number, val: string) { let index = this.hashFunc(key); - this.bucket.set(index, val); + this.bucket[index] = new Entry(index, val); } /* 删除操作 */ public remove(key: number) { let index = this.hashFunc(key); // 置为 null ,代表删除 - this.bucket.set(index, null); + this.bucket[index] = null as any; } /* 获取所有键值对 */ public entrySet(): Entry[] { - return this.bucket.entrySet(); + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i]); + } + } + return arr; } /* 获取所有键 */ public keySet(): number[] { - return this.bucket.keySet(); + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i].key); + } + } + return arr; } /* 获取所有值 */ public valueSet(): string[] { - return this.bucket.valueSet(); + let arr = []; + for (let i = 0; i < this.bucket.length; i++) { + if (this.bucket[i]) { + arr.push(this.bucket[i].val); + } + } + return arr; } } ``` From 18636faf99178d190ec6a0d4af7353387d230986 Mon Sep 17 00:00:00 2001 From: danielsss Date: Thu, 22 Dec 2022 10:44:44 +1100 Subject: [PATCH 09/13] Update built-in Hash Map --- .../chapter_hashing/array_hash_map.ts | 38 +++++++------- codes/typescript/chapter_hashing/hash_map.ts | 49 +++++++++++++++++++ docs/chapter_hashing/hash_map.md | 46 ++++++++--------- 3 files changed, 91 insertions(+), 42 deletions(-) create mode 100644 codes/typescript/chapter_hashing/hash_map.ts diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index c1539e031..dde15185a 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -18,11 +18,11 @@ class Entry { /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - private readonly bucket: Entry[]; + private readonly bucket: Entry | null[]; constructor() { // 初始化一个长度为 100 的桶(数组) - this.bucket = (new Array(100)).fill(null as any); + this.bucket = (new Array(100)).fill(null); } /* 哈希函数 */ @@ -39,20 +39,20 @@ class ArrayHashMap { } /* 添加操作 */ - public put(key: number, val: string) { + public set(key: number, val: string) { let index = this.hashFunc(key); this.bucket[index] = new Entry(index, val); } /* 删除操作 */ - public remove(key: number) { + public delete(key: number) { let index = this.hashFunc(key); // 置为 null ,代表删除 - this.bucket[index] = null as any; + this.bucket[index] = null; } /* 获取所有键值对 */ - public entrySet(): Entry[] { + public entries(): Entry[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { @@ -63,7 +63,7 @@ class ArrayHashMap { } /* 获取所有键 */ - public keySet(): number[] { + public keys(): number[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { @@ -74,7 +74,7 @@ class ArrayHashMap { } /* 获取所有值 */ - public valueSet(): string[] { + public values(): string[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { @@ -86,7 +86,7 @@ class ArrayHashMap { /* 打印哈希表 */ public print() { - let entrySet = this.entrySet(); + let entrySet = this.entries(); for (const entry of entrySet) { if (!entry) continue; console.info(`${entry.key} -> ${entry.val}`); @@ -94,16 +94,16 @@ class ArrayHashMap { } } - +/* Driver Code */ /* 初始化哈希表 */ const map = new ArrayHashMap(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) -map.put(12836, '小哈'); -map.put(15937, '小啰'); -map.put(16750, '小算'); -map.put(13276, '小法'); -map.put(10583, '小鸭'); +map.set(12836, '小哈'); +map.set(15937, '小啰'); +map.set(16750, '小算'); +map.set(13276, '小法'); +map.set(10583, '小鸭'); console.info('\n添加完成后,哈希表为\nKey -> Value'); map.print(); @@ -114,21 +114,21 @@ console.info('\n输入学号 15937 ,查询到姓名 ' + name); /* 删除操作 */ // 在哈希表中删除键值对 (key, value) -map.remove(10583); +map.delete(10583); console.info('\n删除 10583 后,哈希表为\nKey -> Value'); map.print(); /* 遍历哈希表 */ console.info('\n遍历键值对 Key->Value'); -for (const entry of map.entrySet()) { +for (const entry of map.entries()) { console.info(entry.key + ' -> ' + entry.val); } console.info('\n单独遍历键 Key'); -for (const key of map.keySet()) { +for (const key of map.keys()) { console.info(key); } console.info('\n单独遍历值 Value'); -for (const val of map.valueSet()) { +for (const val of map.values()) { console.info(val); } diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts new file mode 100644 index 000000000..3cc968cb6 --- /dev/null +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -0,0 +1,49 @@ +/* + * File: hash_map.ts + * Created Time: 2022-12-20 + * Author: Daniel (better.sunjian@gmail.com) + */ + +class hash_map { + constructor() { + /* 初始化哈希表 */ + const map = new Map(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.set(12836, '小哈'); + map.set(15937, '小啰'); + map.set(16750, '小算'); + map.set(13276, '小法'); + map.set(10583, '小鸭'); + console.info('\n添加完成后,哈希表为\nKey -> Value'); + console.info(map); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + let name = map.get(15937); + console.info('\n输入学号 15937 ,查询到姓名 ' + name); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.delete(10583); + console.info('\n删除 10583 后,哈希表为\nKey -> Value'); + console.info(map); + + /* 遍历哈希表 */ + console.info('\n遍历键值对 Key->Value'); + for (const [k, v] of map.entries()) { + console.info(k + ' -> ' + v); + } + console.info('\n单独遍历键 Key'); + for (const k of map.keys()) { + console.info(k); + } + console.info('\n单独遍历值 Value'); + for (const v of map.values()) { + console.info(v); + } + } +} + +export {}; diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index b3dc7521d..6d289086b 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -139,16 +139,16 @@ comments: true ```typescript title="hash_map.ts" /* 初始化哈希表 */ - const map = new ArrayHashMap(); + const map = new Map(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) - map.put(12836, '小哈'); - map.put(15937, '小啰'); - map.put(16750, '小算'); - map.put(13276, '小法'); - map.put(10583, '小鸭'); + map.set(12836, '小哈'); + map.set(15937, '小啰'); + map.set(16750, '小算'); + map.set(13276, '小法'); + map.set(10583, '小鸭'); console.info('\n添加完成后,哈希表为\nKey -> Value'); - map.print(); + console.info(map); /* 查询操作 */ // 向哈希表输入键 key ,得到值 value @@ -157,9 +157,9 @@ comments: true /* 删除操作 */ // 在哈希表中删除键值对 (key, value) - map.remove(10583); + map.delete(10583); console.info('\n删除 10583 后,哈希表为\nKey -> Value'); - map.print(); + console.info(map); ``` === "C" @@ -256,16 +256,16 @@ comments: true ```typescript title="hash_map.ts" /* 遍历哈希表 */ console.info('\n遍历键值对 Key->Value'); - for (const entry of map.entrySet()) { - console.info(entry.key + ' -> ' + entry.val); + for (const [k, v] of map.entries()) { + console.info(k + ' -> ' + v); } console.info('\n单独遍历键 Key'); - for (const key of map.keySet()) { - console.info(key); + for (const k of map.keys()) { + console.info(k); } console.info('\n单独遍历值 Value'); - for (const val of map.valueSet()) { - console.info(val); + for (const v of map.values()) { + console.info(v); } ``` @@ -525,11 +525,11 @@ $$ /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - private readonly bucket: Entry[]; + private readonly bucket: Entry | null[]; constructor() { // 初始化一个长度为 100 的桶(数组) - this.bucket = (new Array(100)).fill(null as any); + this.bucket = (new Array(100)).fill(null); } /* 哈希函数 */ @@ -546,20 +546,20 @@ $$ } /* 添加操作 */ - public put(key: number, val: string) { + public set(key: number, val: string) { let index = this.hashFunc(key); this.bucket[index] = new Entry(index, val); } /* 删除操作 */ - public remove(key: number) { + public delete(key: number) { let index = this.hashFunc(key); // 置为 null ,代表删除 - this.bucket[index] = null as any; + this.bucket[index] = null; } /* 获取所有键值对 */ - public entrySet(): Entry[] { + public entries(): Entry[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { @@ -570,7 +570,7 @@ $$ } /* 获取所有键 */ - public keySet(): number[] { + public keys(): number[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { @@ -581,7 +581,7 @@ $$ } /* 获取所有值 */ - public valueSet(): string[] { + public values(): string[] { let arr = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { From 572471948583c600ab989b4297cb3c142448860b Mon Sep 17 00:00:00 2001 From: danielsss Date: Mon, 26 Dec 2022 11:14:59 +1100 Subject: [PATCH 10/13] Removed hash_map class --- codes/typescript/chapter_hashing/hash_map.ts | 71 +++++++++----------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts index 3cc968cb6..7e54cf5cb 100644 --- a/codes/typescript/chapter_hashing/hash_map.ts +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -4,46 +4,41 @@ * Author: Daniel (better.sunjian@gmail.com) */ -class hash_map { - constructor() { - /* 初始化哈希表 */ - const map = new Map(); +/* Driver Code */ +/* 初始化哈希表 */ +const map = new Map(); - /* 添加操作 */ - // 在哈希表中添加键值对 (key, value) - map.set(12836, '小哈'); - map.set(15937, '小啰'); - map.set(16750, '小算'); - map.set(13276, '小法'); - map.set(10583, '小鸭'); - console.info('\n添加完成后,哈希表为\nKey -> Value'); - console.info(map); +/* 添加操作 */ +// 在哈希表中添加键值对 (key, value) +map.set(12836, '小哈'); +map.set(15937, '小啰'); +map.set(16750, '小算'); +map.set(13276, '小法'); +map.set(10583, '小鸭'); +console.info('\n添加完成后,哈希表为\nKey -> Value'); +console.info(map); - /* 查询操作 */ - // 向哈希表输入键 key ,得到值 value - let name = map.get(15937); - console.info('\n输入学号 15937 ,查询到姓名 ' + name); +/* 查询操作 */ +// 向哈希表输入键 key ,得到值 value +let name = map.get(15937); +console.info('\n输入学号 15937 ,查询到姓名 ' + name); - /* 删除操作 */ - // 在哈希表中删除键值对 (key, value) - map.delete(10583); - console.info('\n删除 10583 后,哈希表为\nKey -> Value'); - console.info(map); +/* 删除操作 */ +// 在哈希表中删除键值对 (key, value) +map.delete(10583); +console.info('\n删除 10583 后,哈希表为\nKey -> Value'); +console.info(map); - /* 遍历哈希表 */ - console.info('\n遍历键值对 Key->Value'); - for (const [k, v] of map.entries()) { - console.info(k + ' -> ' + v); - } - console.info('\n单独遍历键 Key'); - for (const k of map.keys()) { - console.info(k); - } - console.info('\n单独遍历值 Value'); - for (const v of map.values()) { - console.info(v); - } - } +/* 遍历哈希表 */ +console.info('\n遍历键值对 Key->Value'); +for (const [k, v] of map.entries()) { + console.info(k + ' -> ' + v); +} +console.info('\n单独遍历键 Key'); +for (const k of map.keys()) { + console.info(k); +} +console.info('\n单独遍历值 Value'); +for (const v of map.values()) { + console.info(v); } - -export {}; From 305915270abb99ea9ab1054361221e20d4d3f9ce Mon Sep 17 00:00:00 2001 From: danielsss Date: Mon, 26 Dec 2022 17:01:22 +1100 Subject: [PATCH 11/13] Use tricky method to resolve type check --- .../chapter_hashing/array_hash_map.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index dde15185a..ab35d94c3 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -18,7 +18,7 @@ class Entry { /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - private readonly bucket: Entry | null[]; + private readonly bucket: (Entry | null)[]; constructor() { // 初始化一个长度为 100 的桶(数组) @@ -52,8 +52,8 @@ class ArrayHashMap { } /* 获取所有键值对 */ - public entries(): Entry[] { - let arr = []; + public entries(): (Entry | null)[] { + let arr: (Entry | null)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { arr.push(this.bucket[i]); @@ -63,22 +63,22 @@ class ArrayHashMap { } /* 获取所有键 */ - public keys(): number[] { - let arr = []; + public keys(): (number | undefined)[] { + let arr: (number | undefined)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { - arr.push(this.bucket[i].key); + arr.push(this.bucket[i]?.key); } } return arr; } /* 获取所有值 */ - public values(): string[] { - let arr = []; + public values(): (string | undefined)[] { + let arr: (string | undefined)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { - arr.push(this.bucket[i].val); + arr.push(this.bucket[i]?.val); } } return arr; @@ -121,6 +121,7 @@ map.print(); /* 遍历哈希表 */ console.info('\n遍历键值对 Key->Value'); for (const entry of map.entries()) { + if (!entry) continue; console.info(entry.key + ' -> ' + entry.val); } console.info('\n单独遍历键 Key'); From 528b716766df179e493777667f0ab496490fde79 Mon Sep 17 00:00:00 2001 From: danielsss Date: Mon, 26 Dec 2022 17:15:20 +1100 Subject: [PATCH 12/13] Update hash_map docs --- docs/chapter_hashing/hash_map.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 6d289086b..3c603c58e 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -512,21 +512,20 @@ $$ ```typescript title="array_hash_map.ts" /* 键值对 Number -> String */ class Entry { - - public key: number; - public val: string; - + public key: number; + public val: string; + constructor(key: number, val: string) { this.key = key; this.val = val; } } - + /* 基于数组简易实现的哈希表 */ class ArrayHashMap { - - private readonly bucket: Entry | null[]; - + + private readonly bucket: (Entry | null)[]; + constructor() { // 初始化一个长度为 100 的桶(数组) this.bucket = (new Array(100)).fill(null); @@ -559,8 +558,8 @@ $$ } /* 获取所有键值对 */ - public entries(): Entry[] { - let arr = []; + public entries(): (Entry | null)[] { + let arr: (Entry | null)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { arr.push(this.bucket[i]); @@ -570,22 +569,22 @@ $$ } /* 获取所有键 */ - public keys(): number[] { - let arr = []; + public keys(): (number | undefined)[] { + let arr: (number | undefined)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { - arr.push(this.bucket[i].key); + arr.push(this.bucket[i]?.key); } } return arr; } /* 获取所有值 */ - public values(): string[] { - let arr = []; + public values(): (string | undefined)[] { + let arr: (string | undefined)[] = []; for (let i = 0; i < this.bucket.length; i++) { if (this.bucket[i]) { - arr.push(this.bucket[i].val); + arr.push(this.bucket[i]?.val); } } return arr; From f628fe27d9ce6fb2a2861dc21fcdb5dd9ce70b21 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 26 Dec 2022 23:12:22 +0800 Subject: [PATCH 13/13] Update codes/typescript/chapter_hashing/array_hash_map.ts Co-authored-by: Justin Tse --- codes/typescript/chapter_hashing/array_hash_map.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/typescript/chapter_hashing/array_hash_map.ts b/codes/typescript/chapter_hashing/array_hash_map.ts index ab35d94c3..627205f7f 100644 --- a/codes/typescript/chapter_hashing/array_hash_map.ts +++ b/codes/typescript/chapter_hashing/array_hash_map.ts @@ -41,7 +41,7 @@ class ArrayHashMap { /* 添加操作 */ public set(key: number, val: string) { let index = this.hashFunc(key); - this.bucket[index] = new Entry(index, val); + this.bucket[index] = new Entry(key, val); } /* 删除操作 */