mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 20:00:58 +00:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -513,60 +513,9 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array_hash_map.cs"
|
||||
/* 键值对 int->String */
|
||||
class Entry
|
||||
{
|
||||
public int key;
|
||||
public String val;
|
||||
public Entry(int key, String val)
|
||||
{
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap
|
||||
{
|
||||
private List<Entry?> bucket;
|
||||
public ArrayHashMap()
|
||||
{
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
bucket = new ();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
bucket.Add(null);
|
||||
}
|
||||
}
|
||||
/* 哈希函数 */
|
||||
private int hashFunc(int key)
|
||||
{
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
/* 查询操作 */
|
||||
public String? get(int key)
|
||||
{
|
||||
int index = hashFunc(key);
|
||||
Entry? pair = bucket[index];
|
||||
if (pair == null) return null;
|
||||
return pair.val;
|
||||
}
|
||||
/* 添加操作 */
|
||||
public void put(int key, String val)
|
||||
{
|
||||
Entry pair = new Entry(key, val);
|
||||
int index = hashFunc(key);
|
||||
bucket[index]=pair;
|
||||
}
|
||||
/* 删除操作 */
|
||||
public void remove(int key)
|
||||
{
|
||||
int index = hashFunc(key);
|
||||
// 置为 null ,代表删除
|
||||
bucket[index]=null;
|
||||
}
|
||||
}
|
||||
[class]{Entry}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
Reference in New Issue
Block a user