Files
hello-algo/ja/codes/kotlin/chapter_searching/hashing_search.kt
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

49 lines
1.7 KiB
Kotlin
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.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_searching
import utils.ListNode
/* ハッシュ探索(配列) */
fun hashingSearchArray(map: Map<Int?, Int>, target: Int): Int {
// ハッシュテーブルの key:目的の要素、_val:インデックス
// ハッシュテーブルにこの key が存在しない場合、-1 を返す
return map.getOrDefault(target, -1)
}
/* ハッシュ探索(連結リスト) */
fun hashingSearchLinkedList(map: Map<Int?, ListNode?>, target: Int): ListNode? {
// ハッシュテーブルの key:目的のノードの値、_val:ノードオブジェクト
// ハッシュテーブルにこの key が存在しない場合、null を返す
return map.getOrDefault(target, null)
}
/* 実行用コード */
fun main() {
val target = 3
/* ハッシュ探索(配列) */
val nums = intArrayOf(1, 5, 3, 2, 4, 7, 5, 9, 10, 8)
// ハッシュテーブルを初期化する
val map = HashMap<Int?, Int>()
for (i in nums.indices) {
map[nums[i]] = i // key:要素、_val:インデックス
}
val index = hashingSearchArray(map, target)
println("目的の要素 3 のインデックス = $index")
/* ハッシュ探索(連結リスト) */
var head = ListNode.arrToLinkedList(nums)
// ハッシュテーブルを初期化する
val map1 = HashMap<Int?, ListNode?>()
while (head != null) {
map1[head._val] = head // key:ノードの値、_val:ノード
head = head.next
}
val node = hashingSearchLinkedList(map1, target)
println("目的のノードの値 3 に対応するノードオブジェクトは $node")
}