mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
1. Add build script for Java.
2. Add height limitation for code blocks in extra.css. 3. Fix "节点" to "结点".
This commit is contained in:
@ -416,55 +416,9 @@ $$
|
||||
=== "Java"
|
||||
|
||||
```java title="array_hash_map.java"
|
||||
/* 键值对 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 ArrayList<>();
|
||||
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.get(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.set(index, pair);
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
public void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// 置为 null,代表删除
|
||||
bucket.set(index, null);
|
||||
}
|
||||
}
|
||||
[class]{Entry}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
Reference in New Issue
Block a user