Fix Python code styles.

Update hash_map.
This commit is contained in:
Yudong Jin
2023-02-03 20:03:30 +08:00
parent 15efaca85d
commit a95fe26303
8 changed files with 38 additions and 72 deletions
+1 -1
View File
@@ -144,7 +144,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```python title="array.py"
""" 随机访问元素 """
def randomAccess(nums):
def random_access(nums):
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素
@@ -855,10 +855,10 @@ $$
```python title="space_complexity.py"
""" 线性阶(递归实现) """
def linearRecur(n):
def linear_recur(n):
print("递归 n =", n)
if n == 1: return
linearRecur(n - 1)
linear_recur(n - 1)
```
=== "Go"
+11 -44
View File
@@ -514,7 +514,7 @@ $$
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
// 置为空字符,代表删除
// 置为 nullptr ,代表删除
bucket[index] = nullptr;
}
};
@@ -528,36 +528,36 @@ $$
def __init__(self, key, val):
self.key = key
self.val = val
""" 基于数组简易实现的哈希表 """
class ArrayHashMap:
def __init__(self):
# 初始化一个长度为 100 的桶(数组)
self.bucket = [None] * 100
""" 哈希函数 """
def hashFunc(self, key):
def hash_func(self, key):
index = key % 100
return index
""" 查询操作 """
def get(self, key):
index = self.hashFunc(key)
index = self.hash_func(key)
pair = self.bucket[index]
if pair is None:
return None
return pair.val
""" 添加操作 """
def put(self, key, val):
pair = Entry(key, val)
index = self.hashFunc(key)
index = self.hash_func(key)
self.bucket[index] = pair
""" 删除操作 """
def remove(self, key):
index = self.hashFunc(key)
# 置为空字符,代表删除
index = self.hash_func(key)
# 置为 None ,代表删除
self.bucket[index] = None
```
@@ -708,39 +708,6 @@ $$
// 置为 null ,代表删除
this.bucket[index] = null;
}
/* 获取所有键值对 */
public entries(): (Entry | null)[] {
let arr: (Entry | null)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]);
}
}
return arr;
}
/* 获取所有键 */
public keys(): (number | undefined)[] {
let arr: (number | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.key);
}
}
return arr;
}
/* 获取所有值 */
public values(): (string | undefined)[] {
let arr: (string | undefined)[] = [];
for (let i = 0; i < this.bucket.length; i++) {
if (this.bucket[i]) {
arr.push(this.bucket[i]?.val);
}
}
return arr;
}
}
```