Skip to content

Commit a75f557

Browse files
committed
fix lru_cache function error
1 parent 22d0fe6 commit a75f557

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

docs/03_链表/lru_cache.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def f(n):
6262
只需要让 root 的前一个指向这个被删除节点,然后让之前的最后一个节点也指向它就行了。
6363
6464
使用了循环双端链表之后,我们的操作就都是 O(1) 的了。这也就是使用一个 dict 和一个 循环双端链表 实现LRU 的思路。
65-
不过一般我们使用内置的 OrderedDict(原理和这个类似)就好了,要实现一个循环双端链表是一个不简单的事情。
65+
不过一般我们使用内置的 OrderedDict(原理和这个类似)就好了,要实现一个循环双端链表是一个不简单的事情,因为链表操作很容易出错
6666
67+
补充:其实 lru 有个缺点就是额外的链表比较占用空间,如果你感兴趣的话可以看看 redis 如何实现的 lru 算法
6768
"""
6869

6970

@@ -112,7 +113,7 @@ def _(n):
112113
def f_use_lru(n):
113114
if n <= 1: # 0 or 1
114115
return n
115-
return f(n - 1) + f(n - 2)
116+
return f_use_lru(n - 1) + f_use_lru(n - 2)
116117

117118

118119
def test():

0 commit comments

Comments
 (0)