Update linear_search and hashing_search.

This commit is contained in:
Yudong Jin
2023-02-04 23:49:37 +08:00
parent 62114ce79a
commit f14e3e4c57
22 changed files with 95 additions and 92 deletions

View File

@ -9,14 +9,14 @@ const ListNode = require("../include/ListNode");
/* 哈希查找(数组) */
function hashingSearch(map, target) {
function hashingSearchArray(map, target) {
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
return map.has(target) ? map.get(target) : -1;
}
/* 哈希查找(链表) */
function hashingSearch1(map, target) {
function hashingSearchLinkedList(map, target) {
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.has(target) ? map.get(target) : null;
@ -32,7 +32,7 @@ function main() {
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 元素value: 索引
}
const index = hashingSearch(map, target);
const index = hashingSearchArray(map, target);
console.log("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
@ -43,7 +43,7 @@ function main() {
map1.set(head.val, head); // key: 结点值value: 结点
head = head.next;
}
const node = hashingSearch1(map1, target);
const node = hashingSearchLinkedList(map1, target);
console.log("目标结点值 3 的对应结点对象为" );
PrintUtil.printLinkedList(node);
}