Skip to content

Commit eadf4c8

Browse files
authored
Add kotlin code for the chapter of hashing (krahets#1104)
* feat(kotlin): add kotlin code for dynamic programming. * Update knapsack.kt * feat(kotlin): add kotlin codes for graph. * style(kotlin): reformatted the codes. * feat(kotlin): add kotlin codes for the chapter of greedy. * Update max_product_cutting.kt * feat(kotlin): add kotlin code for chapter of hashing. * style(kotlin): modified some comment * Update array_hash_map.kt * Update hash_map_chaining.kt * Update hash_map_chaining.kt
1 parent cf08173 commit eadf4c8

File tree

6 files changed

+579
-0
lines changed

6 files changed

+579
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* File: array_hash_map.kt
3+
* Created Time: 2024-01-25
4+
* Author: curtishd ([email protected])
5+
*/
6+
7+
package chapter_hashing
8+
9+
/* 键值对 */
10+
class Pair(
11+
var key: Int,
12+
var value: String
13+
)
14+
15+
/* 基于数组实现的哈希表 */
16+
class ArrayHashMap {
17+
private val buckets = arrayOfNulls<Pair>(100)
18+
19+
/* 构造方法 */
20+
init {
21+
// 初始化数组,包含 100 个桶
22+
for (i in 0..<100) {
23+
buckets[i] = null
24+
}
25+
}
26+
27+
/* 哈希函数 */
28+
fun hashFunc(key: Int): Int {
29+
val index = key % 100
30+
return index
31+
}
32+
33+
/* 查询操作 */
34+
fun get(key: Int): String? {
35+
val index = hashFunc(key)
36+
val pair = buckets[index] ?: return null
37+
return pair.value
38+
}
39+
40+
/* 添加操作 */
41+
fun put(key: Int, value: String) {
42+
val pair = Pair(key, value)
43+
val index = hashFunc(key)
44+
buckets[index] = pair
45+
}
46+
47+
/* 删除操作 */
48+
fun remove(key: Int) {
49+
val index = hashFunc(key)
50+
// 置为 null ,代表删除
51+
buckets[index] = null
52+
}
53+
54+
/* 获取所有键值对 */
55+
fun pairSet(): MutableList<Pair> {
56+
val pairSet = ArrayList<Pair>()
57+
for (pair in buckets) {
58+
if (pair != null) pairSet.add(pair)
59+
}
60+
return pairSet
61+
}
62+
63+
/* 获取所有键 */
64+
fun keySet(): MutableList<Int> {
65+
val keySet = ArrayList<Int>()
66+
for (pair in buckets) {
67+
if (pair != null) keySet.add(pair.key)
68+
}
69+
return keySet
70+
}
71+
72+
/* 获取所有值 */
73+
fun valueSet(): MutableList<String> {
74+
val valueSet = ArrayList<String>()
75+
for (pair in buckets) {
76+
pair?.let { valueSet.add(it.value) }
77+
}
78+
return valueSet
79+
}
80+
81+
/* 打印哈希表 */
82+
fun print() {
83+
for (kv in pairSet()) {
84+
val key = kv.key
85+
val value = kv.value
86+
println("${key}->${value}")
87+
}
88+
}
89+
}
90+
91+
/* Driver Code */
92+
fun main() {
93+
/* 初始化哈希表 */
94+
val map = ArrayHashMap()
95+
96+
/* 添加操作 */
97+
// 在哈希表中添加键值对 (key, value)
98+
map.put(12836, "小哈")
99+
map.put(15937, "小啰")
100+
map.put(16750, "小算")
101+
map.put(13276, "小法")
102+
map.put(10583, "小鸭")
103+
println("\n添加完成后,哈希表为\nKey -> Value")
104+
map.print()
105+
106+
/* 查询操作 */
107+
// 向哈希表中输入键 key ,得到值 value
108+
val name: String? = map.get(15937)
109+
println("\n输入学号 15937 ,查询到姓名 $name")
110+
111+
/* 删除操作 */
112+
// 在哈希表中删除键值对 (key, value)
113+
map.remove(10583)
114+
println("\n删除 10583 后,哈希表为\nKey -> Value")
115+
map.print()
116+
117+
/* 遍历哈希表 */
118+
println("\n遍历键值对 Key->Value")
119+
for (kv in map.pairSet()) {
120+
println("${kv.key} -> ${kv.value}")
121+
}
122+
println("\n单独遍历键 Key")
123+
for (key in map.keySet()) {
124+
println(key)
125+
}
126+
println("\n单独遍历值 Value")
127+
for (value in map.valueSet()) {
128+
println(value)
129+
}
130+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* File: built_in_hash.kt
3+
* Created Time: 2024-01-25
4+
* Author: curtishd ([email protected])
5+
*/
6+
7+
package chapter_hashing
8+
9+
import utils.ListNode
10+
11+
/* Driver Code */
12+
fun main() {
13+
val num = 3
14+
val hashNum = Integer.hashCode(num)
15+
println("整数 $num 的哈希值为 $hashNum")
16+
17+
val bol = true
18+
val hashBol = Boolean.hashCode()
19+
println("布尔量 $bol 的哈希值为 $hashBol")
20+
21+
val dec = 3.14159
22+
val hashDec = java.lang.Double.hashCode(dec)
23+
println("小数 $dec 的哈希值为 $hashDec")
24+
25+
val str = "Hello 算法"
26+
val hashStr = str.hashCode()
27+
println("字符串 $str 的哈希值为 $hashStr")
28+
29+
val arr = arrayOf<Any>(12836, "小哈")
30+
val hashTup = arr.contentHashCode()
31+
println("数组 ${arr.contentToString()} 的哈希值为 ${hashTup}")
32+
33+
val obj = ListNode(0)
34+
val hashObj = obj.hashCode()
35+
println("节点对象 $obj 的哈希值为 $hashObj")
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* File: hash_map.kt
3+
* Created Time: 2024-01-25
4+
* Author: curtishd ([email protected])
5+
*/
6+
7+
package chapter_hashing
8+
9+
import utils.printHashMap
10+
11+
/* Driver Code */
12+
fun main() {
13+
/* 初始化哈希表 */
14+
val map: MutableMap<Int, String> = HashMap()
15+
16+
/* 添加操作 */
17+
// 在哈希表中添加键值对 (key, value)
18+
map[12836] = "小哈"
19+
map[15937] = "小啰"
20+
map[16750] = "小算"
21+
map[13276] = "小法"
22+
map[10583] = "小鸭"
23+
println("\n添加完成后,哈希表为\nKey -> Value")
24+
printHashMap(map)
25+
26+
/* 查询操作 */
27+
// 向哈希表中输入键 key ,得到值 value
28+
val name = map[15937]
29+
println("\n输入学号 15937 ,查询到姓名 $name")
30+
31+
/* 删除操作 */
32+
// 在哈希表中删除键值对 (key, value)
33+
map.remove(10583)
34+
println("\n删除 10583 后,哈希表为\nKey -> Value")
35+
printHashMap(map)
36+
37+
/* 遍历哈希表 */
38+
println("\n遍历键值对 Key->Value")
39+
for ((key, value) in map) {
40+
println("$key -> $value")
41+
}
42+
println("\n单独遍历键 Key")
43+
for (key in map.keys) {
44+
println(key)
45+
}
46+
println("\n单独遍历值 Value")
47+
for (value in map.values) {
48+
println(value)
49+
}
50+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* File: hash_map_chaining.kt
3+
* Created Time: 2024-01-25
4+
* Author: curtishd ([email protected])
5+
*/
6+
7+
package chapter_hashing
8+
9+
/* 链式地址哈希表 */
10+
class HashMapChaining() {
11+
var size: Int // 键值对数量
12+
var capacity: Int // 哈希表容量
13+
val loadThres: Double // 触发扩容的负载因子阈值
14+
val extendRatio: Int // 扩容倍数
15+
var buckets: MutableList<MutableList<Pair>> // 桶数组
16+
17+
/* 构造方法 */
18+
init {
19+
size = 0
20+
capacity = 4
21+
loadThres = 2.0 / 3.0
22+
extendRatio = 2
23+
buckets = ArrayList(capacity)
24+
for (i in 0..<capacity) {
25+
buckets.add(mutableListOf())
26+
}
27+
}
28+
29+
/* 哈希函数 */
30+
fun hashFunc(key: Int): Int {
31+
return key % capacity
32+
}
33+
34+
/* 负载因子 */
35+
fun loadFactor(): Double {
36+
return (size / capacity).toDouble()
37+
}
38+
39+
/* 查询操作 */
40+
fun get(key: Int): String? {
41+
val index = hashFunc(key)
42+
val bucket = buckets[index]
43+
// 遍历桶,若找到 key ,则返回对应 val
44+
for (pair in bucket) {
45+
if (pair.key == key) return pair.value
46+
}
47+
// 若未找到 key ,则返回 null
48+
return null
49+
}
50+
51+
/* 添加操作 */
52+
fun put(key: Int, value: String) {
53+
// 当负载因子超过阈值时,执行扩容
54+
if (loadFactor() > loadThres) {
55+
extend()
56+
}
57+
val index = hashFunc(key)
58+
val bucket = buckets[index]
59+
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
60+
for (pair in bucket) {
61+
if (pair.key == key) {
62+
pair.value = value
63+
return
64+
}
65+
}
66+
// 若无该 key ,则将键值对添加至尾部
67+
val pair = Pair(key, value)
68+
bucket.add(pair)
69+
size++
70+
}
71+
72+
/* 删除操作 */
73+
fun remove(key: Int) {
74+
val index = hashFunc(key)
75+
val bucket = buckets[index]
76+
// 遍历桶,从中删除键值对
77+
for (pair in bucket) {
78+
if (pair.key == key) {
79+
bucket.remove(pair)
80+
size--
81+
break
82+
}
83+
}
84+
}
85+
86+
/* 扩容哈希表 */
87+
fun extend() {
88+
// 暂存原哈希表
89+
val bucketsTmp = buckets
90+
// 初始化扩容后的新哈希表
91+
capacity *= extendRatio
92+
// mutablelist 无固定大小
93+
buckets = mutableListOf()
94+
for (i in 0..<capacity) {
95+
buckets.add(mutableListOf())
96+
}
97+
size = 0
98+
// 将键值对从原哈希表搬运至新哈希表
99+
for (bucket in bucketsTmp) {
100+
for (pair in bucket) {
101+
put(pair.key, pair.value)
102+
}
103+
}
104+
}
105+
106+
/* 打印哈希表 */
107+
fun print() {
108+
for (bucket in buckets) {
109+
val res = mutableListOf<String>()
110+
for (pair in bucket) {
111+
val k = pair.key
112+
val v = pair.value
113+
res.add("$k -> $v")
114+
}
115+
println(res)
116+
}
117+
}
118+
}
119+
120+
/* Driver Code */
121+
fun main() {
122+
/* 初始化哈希表 */
123+
val map = HashMapChaining()
124+
125+
/* 添加操作 */
126+
// 在哈希表中添加键值对 (key, value)
127+
map.put(12836, "小哈")
128+
map.put(15937, "小啰")
129+
map.put(16750, "小算")
130+
map.put(13276, "小法")
131+
map.put(10583, "小鸭")
132+
println("\n添加完成后,哈希表为\nKey -> Value")
133+
map.print()
134+
135+
/* 查询操作 */
136+
// 向哈希表中输入键 key ,得到值 value
137+
val name = map.get(13276)
138+
println("\n输入学号 13276 ,查询到姓名 $name")
139+
140+
/* 删除操作 */
141+
// 在哈希表中删除键值对 (key, value)
142+
map.remove(12836)
143+
println("\n删除 12836 后,哈希表为\nKey -> Value")
144+
map.print()
145+
}

0 commit comments

Comments
 (0)