Skip to content

Commit f02c591

Browse files
authored
Create 0705-design-hashset.kt
1 parent 65b9d8e commit f02c591

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

kotlin/0705-design-hashset.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class MyHashSet() {
2+
3+
class LN(val value: Int) {
4+
var next: LN? = null
5+
}
6+
7+
val set = Array (10000) { LN(0) }
8+
9+
fun add(key: Int) {
10+
var cur = set[hash(key)]
11+
while (cur?.next != null) {
12+
if (cur?.next?.value == key)
13+
return
14+
cur = cur?.next!!
15+
}
16+
cur.next = LN(key)
17+
}
18+
19+
fun remove(key: Int) {
20+
var cur = set[hash(key)]
21+
while (cur?.next != null) {
22+
if (cur?.next?.value == key) {
23+
cur?.next = cur?.next?.next
24+
return
25+
}
26+
cur = cur?.next!!
27+
}
28+
}
29+
30+
fun contains(key: Int): Boolean {
31+
var cur = set[hash(key)]
32+
while (cur?.next != null) {
33+
if (cur?.next?.value == key) {
34+
return true
35+
}
36+
cur = cur?.next!!
37+
}
38+
return false
39+
}
40+
41+
private fun hash(key: Int) = key % set.size
42+
43+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy