mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Updated code formats and removed useless codes
This commit is contained in:
		@ -6,134 +6,130 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 键值对 Number -> String */
 | 
					/* 键值对 Number -> String */
 | 
				
			||||||
class Entry {
 | 
					class Entry {
 | 
				
			||||||
  public key: number;
 | 
					    public key: number;
 | 
				
			||||||
  public val: string;
 | 
					    public val: string;
 | 
				
			||||||
  constructor(key: number, val: string) {
 | 
					 | 
				
			||||||
    this.key = key;
 | 
					 | 
				
			||||||
    this.val = val;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 数组的初始化和基本操作 */
 | 
					    constructor(key: number, val: string) {
 | 
				
			||||||
class ArrayList {
 | 
					        this.key = key;
 | 
				
			||||||
 | 
					        this.val = val;
 | 
				
			||||||
  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 {
 | 
					class ArrayHashMap {
 | 
				
			||||||
  // 初始化一个长度为 100 的桶(数组)
 | 
					 | 
				
			||||||
  private bucket: ArrayList;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() {
 | 
					    private readonly bucket: Entry[];
 | 
				
			||||||
    this.bucket = new ArrayList(100);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* 哈希函数 */
 | 
					    constructor() {
 | 
				
			||||||
  private hashFunc(key: number): number {
 | 
					        // 初始化一个长度为 100 的桶(数组)
 | 
				
			||||||
    return key % 100;
 | 
					        this.bucket = (new Array(100)).fill(null as any);
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* 查询操作 */
 | 
					    /* 哈希函数 */
 | 
				
			||||||
  public get(key: number): string | null {
 | 
					    private hashFunc(key: number): number {
 | 
				
			||||||
    let index = this.hashFunc(key);
 | 
					        return key % 100;
 | 
				
			||||||
    let val = this.bucket.get(index);
 | 
					    }
 | 
				
			||||||
    if (val === null) return null;
 | 
					
 | 
				
			||||||
    return val;
 | 
					    /* 查询操作 */
 | 
				
			||||||
  }
 | 
					    public get(key: number): string | null {
 | 
				
			||||||
 | 
					        let index = this.hashFunc(key);
 | 
				
			||||||
  /* 添加操作 */
 | 
					        let entry = this.bucket[index];
 | 
				
			||||||
  public put(key: number, val: string) {
 | 
					        if (entry === null) return null;
 | 
				
			||||||
    let index = this.hashFunc(key);
 | 
					        return entry.val;
 | 
				
			||||||
    this.bucket.set(index, val);
 | 
					    }
 | 
				
			||||||
  }
 | 
					
 | 
				
			||||||
 | 
					    /* 添加操作 */
 | 
				
			||||||
  /* 删除操作 */
 | 
					    public put(key: number, val: string) {
 | 
				
			||||||
  public remove(key: number) {
 | 
					        let index = this.hashFunc(key);
 | 
				
			||||||
    let index = this.hashFunc(key);
 | 
					        this.bucket[index] = new Entry(index, val);
 | 
				
			||||||
    // 置为 null ,代表删除
 | 
					    }
 | 
				
			||||||
    this.bucket.set(index, null);
 | 
					
 | 
				
			||||||
  }
 | 
					    /* 删除操作 */
 | 
				
			||||||
 | 
					    public remove(key: number) {
 | 
				
			||||||
  /* 获取所有键值对 */
 | 
					        let index = this.hashFunc(key);
 | 
				
			||||||
  public entrySet(): Entry[] {
 | 
					        // 置为 null ,代表删除
 | 
				
			||||||
    return this.bucket.entrySet();
 | 
					        this.bucket[index] = null as any;
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* 获取所有键 */
 | 
					    /* 获取所有键值对 */
 | 
				
			||||||
  public keySet(): number[] {
 | 
					    public entrySet(): Entry[] {
 | 
				
			||||||
    return this.bucket.keySet();
 | 
					        let arr = [];
 | 
				
			||||||
  }
 | 
					        for (let i = 0; i < this.bucket.length; i++) {
 | 
				
			||||||
 | 
					            if (this.bucket[i]) {
 | 
				
			||||||
  /* 获取所有值 */
 | 
					                arr.push(this.bucket[i]);
 | 
				
			||||||
  public valueSet(): string[] {
 | 
					            }
 | 
				
			||||||
    return this.bucket.valueSet();
 | 
					        }
 | 
				
			||||||
  }
 | 
					        return arr;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
/* 打印哈希表 */
 | 
					
 | 
				
			||||||
  public print() {
 | 
					    /* 获取所有键 */
 | 
				
			||||||
    let entrySet = this.entrySet();
 | 
					    public keySet(): number[] {
 | 
				
			||||||
    for (const entry of entrySet) {
 | 
					        let arr = [];
 | 
				
			||||||
      if (!entry) continue;
 | 
					        for (let i = 0; i < this.bucket.length; i++) {
 | 
				
			||||||
      console.info(`${entry.key} -> ${entry.val}`);
 | 
					            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 {};
 | 
				
			||||||
 | 
				
			|||||||
@ -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;
 | 
					 | 
				
			||||||
@ -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": ["./**/*"],
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -512,83 +512,24 @@ $$
 | 
				
			|||||||
    ```typescript title="array_hash_map.ts"
 | 
					    ```typescript title="array_hash_map.ts"
 | 
				
			||||||
    /* 键值对 Number -> String */
 | 
					    /* 键值对 Number -> String */
 | 
				
			||||||
    class Entry {
 | 
					    class Entry {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public key: number;
 | 
					        public key: number;
 | 
				
			||||||
        public val: string;
 | 
					        public val: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        constructor(key: number, val: string) {
 | 
					        constructor(key: number, val: string) {
 | 
				
			||||||
            this.key = key;
 | 
					            this.key = key;
 | 
				
			||||||
            this.val = val;
 | 
					            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 {
 | 
					    class ArrayHashMap {
 | 
				
			||||||
        // 初始化一个长度为 100 的桶(数组)
 | 
					
 | 
				
			||||||
        private bucket: ArrayList;
 | 
					        private readonly bucket: Entry[];
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
        constructor() {
 | 
					        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 {
 | 
					        public get(key: number): string | null {
 | 
				
			||||||
            let index = this.hashFunc(key);
 | 
					            let index = this.hashFunc(key);
 | 
				
			||||||
            let val = this.bucket.get(index);
 | 
					            let entry = this.bucket[index];
 | 
				
			||||||
            if (val === null) return null;
 | 
					            if (entry === null) return null;
 | 
				
			||||||
            return val;
 | 
					            return entry.val;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        /* 添加操作 */
 | 
					        /* 添加操作 */
 | 
				
			||||||
        public put(key: number, val: string) {
 | 
					        public put(key: number, val: string) {
 | 
				
			||||||
            let index = this.hashFunc(key);
 | 
					            let index = this.hashFunc(key);
 | 
				
			||||||
            this.bucket.set(index, val);
 | 
					            this.bucket[index] = new Entry(index, val);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        /* 删除操作 */
 | 
					        /* 删除操作 */
 | 
				
			||||||
        public remove(key: number) {
 | 
					        public remove(key: number) {
 | 
				
			||||||
            let index = this.hashFunc(key);
 | 
					            let index = this.hashFunc(key);
 | 
				
			||||||
            // 置为 null ,代表删除
 | 
					            // 置为 null ,代表删除
 | 
				
			||||||
            this.bucket.set(index, null);
 | 
					            this.bucket[index] = null as any;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        /* 获取所有键值对 */
 | 
					        /* 获取所有键值对 */
 | 
				
			||||||
        public entrySet(): Entry[] {
 | 
					        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[] {
 | 
					        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[] {
 | 
					        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;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user