From 01b95bc0f9e34ae6f41c95f380a3ab3ab8d42d7a Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Thu, 29 Dec 2022 03:20:37 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E8=A1=A5=E5=85=85js=E5=92=8Cts=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E5=93=88=E5=B8=8C=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter_searching/hashing_search.js | 51 +++++++++++++++++++ .../chapter_searching/hashing_search.ts | 47 +++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 codes/javascript/chapter_searching/hashing_search.js create mode 100644 codes/typescript/chapter_searching/hashing_search.ts diff --git a/codes/javascript/chapter_searching/hashing_search.js b/codes/javascript/chapter_searching/hashing_search.js new file mode 100644 index 000000000..c1e39766e --- /dev/null +++ b/codes/javascript/chapter_searching/hashing_search.js @@ -0,0 +1,51 @@ +/* + * File: hashing_search.js + * Created Time: 2022-12-29 + * Author: zhuoqinyue (1403450829@qq.com) + */ + +const PrintUtil = require("../include/PrintUtil"); +const ListNode = require("../include/ListNode"); + + +/* 哈希查找(数组) */ +function hashingSearch(map, target) { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; +} + +/* 哈希查找(链表) */ +function hashingSearch1(map, target) { + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; +} + +function main() { + const target = 3; + + /* 哈希查找(数组) */ + const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + // 初始化哈希表 + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], i); // key: 元素,value: 索引 + } + const index = hashingSearch(map, target); + console.log("目标元素 3 的索引 = " + index); + + /* 哈希查找(链表) */ + let head = new ListNode().arrToLinkedList(nums) + // 初始化哈希表 + const map1 = new Map(); + while (head != null) { + map1.set(head.val, head); // key: 结点值,value: 结点 + head = head.next; + } + const node = hashingSearch1(map1, target); + console.log("目标结点值 3 的对应结点对象为" ); + PrintUtil.printLinkedList(node); +} + +main(); diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts new file mode 100644 index 000000000..0d89ee9cd --- /dev/null +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -0,0 +1,47 @@ +/* + * File: hashing_search.js + * Created Time: 2022-12-29 + * Author: zhuoqinyue (1403450829@qq.com) + */ + +/* 哈希查找(数组) */ +function hashingSearch(map: Map, target: number) { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; +} + +/* 哈希查找(链表) */ +function hashingSearch1(map: Map, target: number) { + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; +} + +function main() { + const target = 3; + + /* 哈希查找(数组) */ + const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + // 初始化哈希表 + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], i); // key: 元素,value: 索引 + } + const index = hashingSearch(map, target); + console.log("目标元素 3 的索引 = " + index); + +// /* 哈希查找(链表) */ +// let head = new ListNode().arrToLinkedList(nums) +// // 初始化哈希表 +// const map1 = new Map(); +// while (head != null) { +// map1.set(head.val, head); // key: 结点值,value: 结点 +// head = head.next; +// } +// const node = hashingSearch1(map1, target); +// console.log("目标结点值 3 的对应结点对象为" ); +// printLinkedList(node); +} + +main(); From a7a3618ee0ca708d6653d491bc5fff49256dba3c Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 12:47:33 +0800 Subject: [PATCH 02/11] translate arrToLinkedList method --- .../chapter_searching/hashing_search.ts | 26 +++++++++++-------- codes/typescript/module/ListNode.ts | 15 +++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index 0d89ee9cd..d0ab8bb5c 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -4,6 +4,10 @@ * Author: zhuoqinyue (1403450829@qq.com) */ +import { printLinkedList } from "../module/PrintUtil"; +import ListNode from "../module/ListNode"; + + /* 哈希查找(数组) */ function hashingSearch(map: Map, target: number) { // 哈希表的 key: 目标元素,value: 索引 @@ -31,17 +35,17 @@ function main() { const index = hashingSearch(map, target); console.log("目标元素 3 的索引 = " + index); -// /* 哈希查找(链表) */ -// let head = new ListNode().arrToLinkedList(nums) -// // 初始化哈希表 -// const map1 = new Map(); -// while (head != null) { -// map1.set(head.val, head); // key: 结点值,value: 结点 -// head = head.next; -// } -// const node = hashingSearch1(map1, target); -// console.log("目标结点值 3 的对应结点对象为" ); -// printLinkedList(node); + /* 哈希查找(链表) */ + let head = new ListNode().arrToLinkedList(nums) + // 初始化哈希表 + const map1 = new Map(); + while (head != null) { + map1.set(head.val, head); // key: 结点值,value: 结点 + head = head.next; + } + const node = hashingSearch1(map1, target); + console.log("目标结点值 3 的对应结点对象为"); + printLinkedList(node); } main(); diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts index d6d60616d..a5e303400 100644 --- a/codes/typescript/module/ListNode.ts +++ b/codes/typescript/module/ListNode.ts @@ -14,4 +14,19 @@ export default class ListNode { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } + + /** + * Generate a linked list with an array + * @param arr + * @return + */ + arrToLinkedList(arr: number[]) { + const dum: ListNode = new ListNode(0); + let head = dum; + for (const val of arr) { + head.next = new ListNode(val); + head = head.next; + } + return dum.next; + } } From 01a6fcef208d4d1ff5fb88259cccb61410c98a89 Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 13:08:30 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84ts=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/typescript/module/ListNode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts index a5e303400..ef7da6631 100644 --- a/codes/typescript/module/ListNode.ts +++ b/codes/typescript/module/ListNode.ts @@ -20,7 +20,7 @@ export default class ListNode { * @param arr * @return */ - arrToLinkedList(arr: number[]) { + arrToLinkedList(arr: number[]): ListNode | null { const dum: ListNode = new ListNode(0); let head = dum; for (const val of arr) { From 5cfcba1eb9bf6d54438790dcb480e6416944f62a Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 13:39:19 +0800 Subject: [PATCH 04/11] update the doc --- docs/chapter_searching/hashing_search.md | 30 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index a52f72e21..1e41297e7 100644 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -68,13 +68,23 @@ comments: true === "JavaScript" ```js title="hashing_search.js" - + /* 哈希查找(数组) */ + function hashingSearch(map, target) { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; + } ``` === "TypeScript" ```typescript title="hashing_search.ts" - + /* 哈希查找(数组) */ + function hashingSearch(map: Map, target: number) { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; + } ``` === "C" @@ -145,19 +155,29 @@ comments: true } else { return nil } - } + } ``` === "JavaScript" ```js title="hashing_search.js" - + /* 哈希查找(链表) */ + function hashingSearch1(map, target) { + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; + } ``` === "TypeScript" ```typescript title="hashing_search.ts" - + /* 哈希查找(链表) */ + function hashingSearch1(map: Map, target: number) { + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; + } ``` === "C" From 8804ab2940fb0ed660c9f55234a7921a795bcfe6 Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 20:30:55 +0800 Subject: [PATCH 05/11] update the indent --- .../chapter_searching/hashing_search.js | 54 +++++++++---------- .../chapter_searching/hashing_search.ts | 54 +++++++++---------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/codes/javascript/chapter_searching/hashing_search.js b/codes/javascript/chapter_searching/hashing_search.js index c1e39766e..4b5923d4a 100644 --- a/codes/javascript/chapter_searching/hashing_search.js +++ b/codes/javascript/chapter_searching/hashing_search.js @@ -10,42 +10,42 @@ const ListNode = require("../include/ListNode"); /* 哈希查找(数组) */ function hashingSearch(map, target) { - // 哈希表的 key: 目标元素,value: 索引 - // 若哈希表中无此 key ,返回 -1 - return map.has(target) ? map.get(target) : -1; + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; } /* 哈希查找(链表) */ function hashingSearch1(map, target) { - // 哈希表的 key: 目标结点值,value: 结点对象 - // 若哈希表中无此 key ,返回 null - return map.has(target) ? map.get(target) : null; + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; } function main() { - const target = 3; + const target = 3; - /* 哈希查找(数组) */ - const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; - // 初始化哈希表 - const map = new Map(); - for (let i = 0; i < nums.length; i++) { - map.set(nums[i], i); // key: 元素,value: 索引 - } - const index = hashingSearch(map, target); - console.log("目标元素 3 的索引 = " + index); + /* 哈希查找(数组) */ + const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + // 初始化哈希表 + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], i); // key: 元素,value: 索引 + } + const index = hashingSearch(map, target); + console.log("目标元素 3 的索引 = " + index); - /* 哈希查找(链表) */ - let head = new ListNode().arrToLinkedList(nums) - // 初始化哈希表 - const map1 = new Map(); - while (head != null) { - map1.set(head.val, head); // key: 结点值,value: 结点 - head = head.next; - } - const node = hashingSearch1(map1, target); - console.log("目标结点值 3 的对应结点对象为" ); - PrintUtil.printLinkedList(node); + /* 哈希查找(链表) */ + let head = new ListNode().arrToLinkedList(nums) + // 初始化哈希表 + const map1 = new Map(); + while (head != null) { + map1.set(head.val, head); // key: 结点值,value: 结点 + head = head.next; + } + const node = hashingSearch1(map1, target); + console.log("目标结点值 3 的对应结点对象为" ); + PrintUtil.printLinkedList(node); } main(); diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index d0ab8bb5c..064201737 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -10,42 +10,42 @@ import ListNode from "../module/ListNode"; /* 哈希查找(数组) */ function hashingSearch(map: Map, target: number) { - // 哈希表的 key: 目标元素,value: 索引 - // 若哈希表中无此 key ,返回 -1 - return map.has(target) ? map.get(target) : -1; + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + return map.has(target) ? map.get(target) : -1; } /* 哈希查找(链表) */ function hashingSearch1(map: Map, target: number) { - // 哈希表的 key: 目标结点值,value: 结点对象 - // 若哈希表中无此 key ,返回 null - return map.has(target) ? map.get(target) : null; + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 null + return map.has(target) ? map.get(target) : null; } function main() { - const target = 3; + const target = 3; - /* 哈希查找(数组) */ - const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; - // 初始化哈希表 - const map = new Map(); - for (let i = 0; i < nums.length; i++) { - map.set(nums[i], i); // key: 元素,value: 索引 - } - const index = hashingSearch(map, target); - console.log("目标元素 3 的索引 = " + index); + /* 哈希查找(数组) */ + const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; + // 初始化哈希表 + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], i); // key: 元素,value: 索引 + } + const index = hashingSearch(map, target); + console.log("目标元素 3 的索引 = " + index); - /* 哈希查找(链表) */ - let head = new ListNode().arrToLinkedList(nums) - // 初始化哈希表 - const map1 = new Map(); - while (head != null) { - map1.set(head.val, head); // key: 结点值,value: 结点 - head = head.next; - } - const node = hashingSearch1(map1, target); - console.log("目标结点值 3 的对应结点对象为"); - printLinkedList(node); + /* 哈希查找(链表) */ + let head = new ListNode().arrToLinkedList(nums) + // 初始化哈希表 + const map1 = new Map(); + while (head != null) { + map1.set(head.val, head); // key: 结点值,value: 结点 + head = head.next; + } + const node = hashingSearch1(map1, target); + console.log("目标结点值 3 的对应结点对象为"); + printLinkedList(node); } main(); From 742b6b632fbbef1818f056f644f3553a75db0027 Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 20:35:17 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E5=A4=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/javascript/chapter_searching/hashing_search.js | 2 +- codes/typescript/chapter_searching/hashing_search.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_searching/hashing_search.js b/codes/javascript/chapter_searching/hashing_search.js index 4b5923d4a..96b451873 100644 --- a/codes/javascript/chapter_searching/hashing_search.js +++ b/codes/javascript/chapter_searching/hashing_search.js @@ -1,4 +1,4 @@ -/* +/** * File: hashing_search.js * Created Time: 2022-12-29 * Author: zhuoqinyue (1403450829@qq.com) diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index 064201737..ab5beb701 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -1,4 +1,4 @@ -/* +/** * File: hashing_search.js * Created Time: 2022-12-29 * Author: zhuoqinyue (1403450829@qq.com) From b7ff82deb5d0f45552f8587b321ed34d17e45f96 Mon Sep 17 00:00:00 2001 From: steak-zhuo Date: Sun, 8 Jan 2023 20:39:42 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BD=9C=E8=80=85?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/javascript/chapter_searching/hashing_search.js | 2 +- codes/typescript/chapter_searching/hashing_search.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_searching/hashing_search.js b/codes/javascript/chapter_searching/hashing_search.js index 96b451873..ebe6a139f 100644 --- a/codes/javascript/chapter_searching/hashing_search.js +++ b/codes/javascript/chapter_searching/hashing_search.js @@ -1,7 +1,7 @@ /** * File: hashing_search.js * Created Time: 2022-12-29 - * Author: zhuoqinyue (1403450829@qq.com) + * Author: Zhuo Qinyue (1403450829@qq.com) */ const PrintUtil = require("../include/PrintUtil"); diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index ab5beb701..ef0245a91 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -1,7 +1,7 @@ /** * File: hashing_search.js * Created Time: 2022-12-29 - * Author: zhuoqinyue (1403450829@qq.com) + * Author: Zhuo Qinyue (1403450829@qq.com) */ import { printLinkedList } from "../module/PrintUtil"; From 55089726d607ff29f905588b49694ce011eb0f3d Mon Sep 17 00:00:00 2001 From: zhuoqinyue <64182179+zhuoqinyue@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:12:57 +0800 Subject: [PATCH 08/11] Update codes/typescript/chapter_searching/hashing_search.ts Co-authored-by: Justin Tse --- codes/typescript/chapter_searching/hashing_search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index ef0245a91..6133942b7 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -16,10 +16,10 @@ function hashingSearch(map: Map, target: number) { } /* 哈希查找(链表) */ -function hashingSearch1(map: Map, target: number) { +function hashingSearch1(map: Map, target: number): ListNode | null { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null - return map.has(target) ? map.get(target) : null; + return map.has(target) ? map.get(target) as ListNode : null; } function main() { From c6f6fa00159955efe1046a881455a5b7d60e5b36 Mon Sep 17 00:00:00 2001 From: zhuoqinyue <64182179+zhuoqinyue@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:13:06 +0800 Subject: [PATCH 09/11] Update docs/chapter_searching/hashing_search.md Co-authored-by: Justin Tse --- docs/chapter_searching/hashing_search.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index 1e41297e7..19cb2c8e4 100644 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -173,10 +173,10 @@ comments: true ```typescript title="hashing_search.ts" /* 哈希查找(链表) */ - function hashingSearch1(map: Map, target: number) { + function hashingSearch1(map: Map, target: number): ListNode | null { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null - return map.has(target) ? map.get(target) : null; + return map.has(target) ? map.get(target) as ListNode : null; } ``` From 4d542be9d4fe8e64d45a4db34ddc537a26d265ad Mon Sep 17 00:00:00 2001 From: zhuoqinyue <64182179+zhuoqinyue@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:13:16 +0800 Subject: [PATCH 10/11] Update codes/typescript/chapter_searching/hashing_search.ts Co-authored-by: Justin Tse --- codes/typescript/chapter_searching/hashing_search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index 6133942b7..7f93d7178 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -9,10 +9,10 @@ import ListNode from "../module/ListNode"; /* 哈希查找(数组) */ -function hashingSearch(map: Map, target: number) { +function hashingSearch(map: Map, target: number): number { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 - return map.has(target) ? map.get(target) : -1; + return map.has(target) ? map.get(target) as number : -1; } /* 哈希查找(链表) */ From 9adc78a3fe02e855d87f8e4865a0309cbb3e8839 Mon Sep 17 00:00:00 2001 From: zhuoqinyue <64182179+zhuoqinyue@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:13:24 +0800 Subject: [PATCH 11/11] Update docs/chapter_searching/hashing_search.md Co-authored-by: Justin Tse --- docs/chapter_searching/hashing_search.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index 19cb2c8e4..37b5032d6 100644 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -80,10 +80,10 @@ comments: true ```typescript title="hashing_search.ts" /* 哈希查找(数组) */ - function hashingSearch(map: Map, target: number) { + function hashingSearch(map: Map, target: number): number { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 - return map.has(target) ? map.get(target) : -1; + return map.has(target) ? map.get(target) as number : -1; } ```