Skip to content

Commit d211c9c

Browse files
committed
Deployed 7524e5b with MkDocs version: 1.0.4
1 parent 2da4978 commit d211c9c

File tree

9 files changed

+36
-13
lines changed

9 files changed

+36
-13
lines changed
Binary file not shown.
Binary file not shown.

03_链表/linked_list.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def remove(self, value): # O(n)
7777
if curnode.value == value:
7878
prevnode.next = curnode.next
7979
if curnode is self.tailnode: # NOTE: 注意更新 tailnode
80-
self.tailnode = prevnode
80+
if prevnode is self.root:
81+
self.tailnode = None
82+
else:
83+
self.tailnode = prevnode
8184
del curnode
8285
self.length -= 1
8386
return 1 # 表明删除成功
@@ -185,6 +188,13 @@ def test_linked_list_remove():
185188
ll.remove(7)
186189
print(list(ll))
187190

191+
def test_single_node():
192+
# https://github.com/PegasusWang/python_data_structures_and_algorithms/pull/21
193+
ll = LinkedList()
194+
ll.append(0)
195+
ll.remove(0)
196+
ll.appendleft(1)
197+
assert list(ll) == [1]
188198

189199
def test_linked_list_reverse():
190200
ll = LinkedList()
@@ -203,6 +213,7 @@ def test_linked_list_append():
203213

204214

205215
if __name__ == '__main__':
216+
test_single_node()
206217
test_linked_list()
207218
test_linked_list_append()
208219
test_linked_list_reverse()
Binary file not shown.

07_哈希表/hashtable.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def __iter__(self):
2828

2929

3030
class Slot(object):
31-
"""定义一个 hash 表 数组的槽
32-
注意,一个槽有三种状态,看你能否想明白。相比链接法解决冲突,二次探查法删除一个 key 的操作稍微复杂。
31+
"""定义一个 hash 表数组的槽(slot 这里指的就是数组的一个位置)
32+
hash table 就是一个 数组,每个数组的元素(也叫slot槽)是一个对象,对象包含两个属性 key 和 value。
33+
34+
注意,一个槽有三种状态,看你能否想明白。相比链接法解决冲突,探查法删除一个 key 的操作稍微复杂。
3335
1.从未使用 HashMap.UNUSED。此槽没有被使用和冲突过,查找时只要找到 UNUSED 就不用再继续探查了
3436
2.使用过但是 remove 了,此时是 HashMap.EMPTY,该探查点后边的元素仍然可能是有key的,需要继续查找
3537
3.槽正在使用 Slot 节点
@@ -60,23 +62,33 @@ def _hash(self, key):
6062
return abs(hash(key)) % len(self._table)
6163

6264
def _find_key(self, key):
63-
index = self._hash(key)
65+
"""
66+
解释一个 slot 为 UNUSED 和 EMPTY 的区别
67+
因为使用的是二次探查的方式,假如有两个元素 A,B 冲突了,首先A hash 得到是 slot 下标5,A 放到了第5个槽,之后插入 B 因为冲突了,所以继续根据二次探查方式放到了 slot8。
68+
然后删除 A,槽 5 被置为 EMPTY。然后我去查找 B,第一次 hash 得到的是 槽5,但是这个时候我还是需要第二次计算 hash 才能找到 B。但是如果槽是 UNUSED 我就不用继续找了,我认为 B 就是不存在的元素。这个就是 UNUSED 和 EMPTY 的区别。
69+
"""
70+
origin_index = index = self._hash(key) # origin_index 判断是否又走到了起点,如果查找一圈了都找不到则无此元素
6471
_len = len(self._table)
6572
while self._table[index] is not HashTable.UNUSED:
66-
if self._table[index] is HashTable.EMPTY:
67-
index = (index*5 + 1) % _len
73+
if self._table[index] is HashTable.EMPTY: # 注意如果是 EMPTY,继续寻找下一个槽
74+
index = (index * 5 + 1) % _len
75+
if index == origin_index:
76+
break
6877
continue
69-
elif self._table[index].key == key:
78+
if self._table[index].key == key: # 找到了key
7079
return index
7180
else:
72-
index = (index*5 + 1) % _len
81+
index = (index * 5 + 1) % _len # 没有找到继续找下一个位置
82+
if index == origin_index:
83+
break
84+
7385
return None
7486

7587
def _find_slot_for_insert(self, key):
7688
index = self._hash(key)
7789
_len = len(self._table)
78-
while not self._slot_can_insert(index):
79-
index = (index*5 + 1) % _len
90+
while not self._slot_can_insert(index): # 直到找到一个可以用的槽
91+
index = (index * 5 + 1) % _len
8092
return index
8193

8294
def _slot_can_insert(self, index):
Binary file not shown.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ <h2 id="_21">如何提问?</h2>
512512
<p>如果读者关于代码、视频、讲义有任何疑问,欢迎一起讨论
513513
请注意以下几点:</p>
514514
<ul>
515-
<li>优先在网易云课堂的讨论区提问,方便别的同学浏览。如果未购买视频,也可以直接在 github 里提出 issue,笔者有空会给大家解答和讨论。</li>
516515
<li>描述尽量具体,视频或者代码哪一部分有问题?请尽量把涉及章节和代码贴出来,方便定位问题。</li>
517516
<li>如果涉及到代码,提问时请保持代码的格式</li>
517+
<li>如果直接提了代码bug,最好有相关测试用例展示失败 test case,方便复现问题</li>
518518
</ul>
519519
<h2 id="_22">本电子书制作和写作方式</h2>
520520
<p>使用 mkdocs 和 markdown 构建,使用 Python-Markdown-Math 完成数学公式。
@@ -587,5 +587,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
587587

588588
<!--
589589
MkDocs version : 1.0.4
590-
Build Date UTC : 2019-10-20 03:09:16
590+
Build Date UTC : 2019-10-20 10:02:13
591591
-->

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)