File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments