Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf02c591

Browse files
authored
Create 0705-design-hashset.kt
1 parent65b9d8e commitf02c591

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+
classMyHashSet() {
2+
3+
classLN(valvalue:Int) {
4+
var next:LN?=null
5+
}
6+
7+
val set=Array (10000) {LN(0) }
8+
9+
funadd(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+
funremove(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+
funcontains(key:Int):Boolean {
31+
var cur= set[hash(key)]
32+
while (cur?.next!=null) {
33+
if (cur?.next?.value== key) {
34+
returntrue
35+
}
36+
cur= cur?.next!!
37+
}
38+
returnfalse
39+
}
40+
41+
privatefunhash(key:Int)= key% set.size
42+
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp