Files
hello-algo/ja/codes/csharp/chapter_searching/hashing_search.cs
Yudong Jin d7b2277d2b Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
2026-03-30 07:30:15 +08:00

51 lines
1.9 KiB
C#
Raw Permalink 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.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_searching;
public class hashing_search {
/* ハッシュ探索(配列) */
int HashingSearchArray(Dictionary<int, int> map, int target) {
// ハッシュテーブルの key: 目標要素、value: インデックス
// ハッシュテーブルにこの key がなければ -1 を返す
return map.GetValueOrDefault(target, -1);
}
/* ハッシュ探索(連結リスト) */
ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
// ハッシュテーブルの key: 目標ード値、value: ノードオブジェクト
// ハッシュテーブルにこの key がなければ null を返す
return map.GetValueOrDefault(target);
}
[Test]
public void Test() {
int target = 3;
/* ハッシュ探索(配列) */
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// ハッシュテーブルを初期化
Dictionary<int, int> map = [];
for (int i = 0; i < nums.Length; i++) {
map[nums[i]] = i; // key: 要素、value: インデックス
}
int index = HashingSearchArray(map, target);
Console.WriteLine("対象要素 3 のインデックス = " + index);
/* ハッシュ探索(連結リスト) */
ListNode? head = ListNode.ArrToLinkedList(nums);
// ハッシュテーブルを初期化
Dictionary<int, ListNode> map1 = [];
while (head != null) {
map1[head.val] = head; // key: ード値、value: ノード
head = head.next;
}
ListNode? node = HashingSearchLinkedList(map1, target);
Console.WriteLine("目標ノード値 3 に対応するノードオブジェクトは " + node);
}
}