From 528b716766df179e493777667f0ab496490fde79 Mon Sep 17 00:00:00 2001 From: danielsss Date: Mon, 26 Dec 2022 17:15:20 +1100 Subject: [PATCH] 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;