mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 22:57:48 +08:00
46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
/**
|
||
* File: hashing_search.js
|
||
* Created Time: 2022-12-29
|
||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||
*/
|
||
|
||
const { arrToLinkedList } = require('../modules/ListNode');
|
||
|
||
/* ハッシュ探索(配列) */
|
||
function hashingSearchArray(map, target) {
|
||
// ハッシュテーブルの key: 目標要素、value: インデックス
|
||
// ハッシュテーブルにこの key がなければ -1 を返す
|
||
return map.has(target) ? map.get(target) : -1;
|
||
}
|
||
|
||
/* ハッシュ探索(連結リスト) */
|
||
function hashingSearchLinkedList(map, target) {
|
||
// ハッシュテーブルの key: 目標ノード値、value: ノードオブジェクト
|
||
// ハッシュテーブルにこの key がなければ null を返す
|
||
return map.has(target) ? map.get(target) : null;
|
||
}
|
||
|
||
/* Driver Code */
|
||
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 = hashingSearchArray(map, target);
|
||
console.log('目標要素 3 のインデックス = ' + index);
|
||
|
||
/* ハッシュ探索(連結リスト) */
|
||
let head = arrToLinkedList(nums);
|
||
// ハッシュテーブルを初期化
|
||
const map1 = new Map();
|
||
while (head != null) {
|
||
map1.set(head.val, head); // key: ノード値、value: ノード
|
||
head = head.next;
|
||
}
|
||
const node = hashingSearchLinkedList(map1, target);
|
||
console.log('目標ノード値 3 に対応するノードオブジェクトは', node);
|