Files
hello-algo/ja/codes/cpp/chapter_searching/hashing_search.cpp
Ikko Eltociear Ashimine 954c45864b docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
2025-10-17 05:04:43 +08:00

53 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: hashing_search.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* ハッシュ探索(配列) */
int hashingSearchArray(unordered_map<int, int> map, int target) {
// ハッシュテーブルのキー:ターゲット要素、値:インデックス
// ハッシュテーブルにこのキーが含まれていない場合、-1を返す
if (map.find(target) == map.end())
return -1;
return map[target];
}
/* ハッシュ探索(連結リスト) */
ListNode *hashingSearchLinkedList(unordered_map<int, ListNode *> map, int target) {
// ハッシュテーブルのキー:ターゲットノード値、値:ノードオブジェクト
// キーがハッシュテーブルにない場合、nullptrを返す
if (map.find(target) == map.end())
return nullptr;
return map[target];
}
/* ドライバコード */
int main() {
int target = 3;
/* ハッシュ探索(配列) */
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
// ハッシュテーブルを初期化
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]] = i; // キー:要素、値:インデックス
}
int index = hashingSearchArray(map, target);
cout << "ターゲット要素3のインデックスは " << index << " です" << endl;
/* ハッシュ探索(連結リスト) */
ListNode *head = vecToLinkedList(nums);
// ハッシュテーブルを初期化
unordered_map<int, ListNode *> map1;
while (head != nullptr) {
map1[head->val] = head; // キー:ノード値、値:ノード
head = head->next;
}
ListNode *node = hashingSearchLinkedList(map1, target);
cout << "ターゲットード値3に対応するードオブジェクトは " << node << " です" << endl;
return 0;
}