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

Commite183122

Browse files
committed
Added 0706-design-hashmap.cs
1 parent027cb6c commite183122

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

‎csharp/0706-design-hashmap.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
publicclassMyHashMap
2+
{
3+
privateclassLinkedListNode
4+
{
5+
publicLinkedListNode(intkey,intval,LinkedListNodenext)
6+
{
7+
this.Key=key;
8+
this.Val=val;
9+
this.Next=next;
10+
}
11+
12+
publicintKey{get;set;}
13+
publicintVal{get;set;}
14+
publicLinkedListNodeNext{get;set;}
15+
}
16+
17+
publicMyHashMap()
18+
{
19+
Map=newLinkedListNode[10000];
20+
}
21+
22+
publicintHash(intkey)
23+
{
24+
returnkey%Map.Length;
25+
}
26+
27+
publicvoidPut(intkey,intvalue)
28+
{
29+
intindex=Hash(key);
30+
if(Map[index]==null)
31+
Map[index]=newLinkedListNode(key,value,null);
32+
else
33+
{
34+
LinkedListNodenode=Map[index];
35+
while(true)
36+
{
37+
if(node.Key==key)
38+
{
39+
node.Val=value;
40+
return;
41+
}
42+
if(node.Next==null)
43+
break;
44+
else
45+
node=node.Next;
46+
}
47+
node.Next=newLinkedListNode(key,value,null);
48+
}
49+
}
50+
51+
publicintGet(intkey)
52+
{
53+
intindex=Hash(key);
54+
LinkedListNodenode=Map[index];
55+
if(node!=null)
56+
{
57+
while(true)
58+
{
59+
if(node.Key==key)
60+
returnnode.Val;
61+
62+
if(node.Next==null)
63+
break;
64+
else
65+
node=node.Next;
66+
}
67+
}
68+
69+
return-1;
70+
}
71+
72+
publicvoidRemove(intkey)
73+
{
74+
intindex=Hash(key);
75+
LinkedListNodenode=Map[index];
76+
if(node==null)
77+
return;
78+
if(node.Key==key)
79+
{
80+
Map[index]=node.Next;
81+
node=null;
82+
return;
83+
}
84+
while(node.Next!=null)
85+
{
86+
if(node.Next.Key==key)
87+
{
88+
node.Next=node.Next.Next;
89+
return;
90+
}
91+
92+
node=node.Next;
93+
}
94+
}
95+
96+
privateLinkedListNode[]Map{get;set;}
97+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp