mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
doc(code): modify go code in docs
This commit is contained in:
@@ -524,30 +524,30 @@ $$
|
||||
|
||||
```go title="array_hash_map.go"
|
||||
/* 键值对 int->String */
|
||||
type Entry struct {
|
||||
type entry struct {
|
||||
key int
|
||||
val string
|
||||
}
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
type ArrayHashMap struct {
|
||||
bucket []*Entry
|
||||
type arrayHashMap struct {
|
||||
bucket []*entry
|
||||
}
|
||||
|
||||
func newArrayHashMap() *ArrayHashMap {
|
||||
func newArrayHashMap() *arrayHashMap {
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
bucket := make([]*Entry, 100)
|
||||
return &ArrayHashMap{bucket: bucket}
|
||||
bucket := make([]*entry, 100)
|
||||
return &arrayHashMap{bucket: bucket}
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
func (a *ArrayHashMap) hashFunc(key int) int {
|
||||
func (a *arrayHashMap) hashFunc(key int) int {
|
||||
index := key % 100
|
||||
return index
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
func (a *ArrayHashMap) get(key int) string {
|
||||
func (a *arrayHashMap) get(key int) string {
|
||||
index := a.hashFunc(key)
|
||||
pair := a.bucket[index]
|
||||
if pair == nil {
|
||||
@@ -557,16 +557,16 @@ $$
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
func (a *ArrayHashMap) put(key int, val string) {
|
||||
pair := &Entry{key: key, val: val}
|
||||
func (a *arrayHashMap) put(key int, val string) {
|
||||
pair := &entry{key: key, val: val}
|
||||
index := a.hashFunc(key)
|
||||
a.bucket[index] = pair
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
func (a *ArrayHashMap) remove(key int) {
|
||||
func (a *arrayHashMap) remove(key int) {
|
||||
index := a.hashFunc(key)
|
||||
// 置为空字符,代表删除
|
||||
// 置为 nil ,代表删除
|
||||
a.bucket[index] = nil
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user