Files
hello-algo/ru/codes/cpp/chapter_searching/hashing_search.cpp
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

54 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) {
// key хеш-таблицы: целевой элемент, value: индекс
// Если такого key нет в хеш-таблице, вернуть -1
if (map.find(target) == map.end())
return -1;
return map[target];
}
/* Хеш-поиск (связный список) */
ListNode *hashingSearchLinkedList(unordered_map<int, ListNode *> map, int target) {
// key хеш-таблицы: значение целевого узла, value: объект узла
// Если такого key нет в хеш-таблице, вернуть nullptr
if (map.find(target) == map.end())
return nullptr;
return map[target];
}
/* Driver Code */
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; // key: элемент, value: индекс
}
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; // key: значение узла, value: узел
head = head->next;
}
ListNode *node = hashingSearchLinkedList(map1, target);
cout << "Объект узла со значением 3 = " << node << endl;
return 0;
}