mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 11:06:29 +08:00
fixed: several bugs
This commit is contained in:
parent
6eec01d594
commit
bd21fd8be9
3 changed files with 77 additions and 20 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* 键值对 int->String */
|
/* 键值对 Number -> String */
|
||||||
class Entry {
|
class Entry {
|
||||||
public key: number;
|
public key: number;
|
||||||
public val: string;
|
public val: string;
|
||||||
|
@ -26,19 +26,23 @@ class ArrayList {
|
||||||
public set(key: number, val: string | null) {
|
public set(key: number, val: string | null) {
|
||||||
if (val !== null) {
|
if (val !== null) {
|
||||||
this.elements[key] = new Entry(key, val);
|
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 {
|
public get(key: number): string | null {
|
||||||
return this.elements[key].val;
|
if (this.elements[key] instanceof Entry) {
|
||||||
|
return this.elements[key].val;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public entrySet() {
|
public entrySet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i]);
|
arr.push(this.elements[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +52,7 @@ class ArrayList {
|
||||||
public valueSet() {
|
public valueSet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i].val);
|
arr.push(this.elements[i].val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +62,7 @@ class ArrayList {
|
||||||
public keySet() {
|
public keySet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i].key);
|
arr.push(this.elements[i].key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,9 +88,7 @@ class ArrayHashMap {
|
||||||
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 val = this.bucket.get(index);
|
||||||
if (val === null) {
|
if (val === null) return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +124,7 @@ class ArrayHashMap {
|
||||||
public print() {
|
public print() {
|
||||||
let entrySet = this.entrySet();
|
let entrySet = this.entrySet();
|
||||||
for (const entry of entrySet) {
|
for (const entry of entrySet) {
|
||||||
|
if (!entry) continue;
|
||||||
console.info(`${entry.key} -> ${entry.val}`);
|
console.info(`${entry.key} -> ${entry.val}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,8 @@
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"module": "esnext",
|
"module": "commonjs",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"importHelpers": true,
|
|
||||||
"noEmit": false,
|
"noEmit": false,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
|
|
|
@ -510,7 +510,7 @@ $$
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="array_hash_map.ts"
|
```typescript title="array_hash_map.ts"
|
||||||
/* 键值对 int->String */
|
/* 键值对 Number -> String */
|
||||||
class Entry {
|
class Entry {
|
||||||
public key: number;
|
public key: number;
|
||||||
public val: string;
|
public val: string;
|
||||||
|
@ -538,19 +538,23 @@ $$
|
||||||
public set(key: number, val: string | null) {
|
public set(key: number, val: string | null) {
|
||||||
if (val !== null) {
|
if (val !== null) {
|
||||||
this.elements[key] = new Entry(key, val);
|
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 {
|
public get(key: number): string | null {
|
||||||
return this.elements[key].val;
|
if (this.elements[key] instanceof Entry) {
|
||||||
|
return this.elements[key].val;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public entrySet() {
|
public entrySet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i]);
|
arr.push(this.elements[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -560,7 +564,7 @@ $$
|
||||||
public valueSet() {
|
public valueSet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i].val);
|
arr.push(this.elements[i].val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -570,13 +574,64 @@ $$
|
||||||
public keySet() {
|
public keySet() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
for (let i = 0; i < this.elements.length; i++) {
|
for (let i = 0; i < this.elements.length; i++) {
|
||||||
if (this.elements[i] !== null) {
|
if (this.elements[i]) {
|
||||||
arr.push(this.elements[i].key);
|
arr.push(this.elements[i].key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arr;
|
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"
|
=== "C"
|
||||||
|
|
Loading…
Reference in a new issue