Skip to content

Commit 8864ab4

Browse files
committed
Deployed cd14876 with MkDocs version: 1.0.4
1 parent d12b32d commit 8864ab4

File tree

6 files changed

+123
-26
lines changed

6 files changed

+123
-26
lines changed

03_链表/lru_cache.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,83 @@ def test():
132132

133133
if __name__ == '__main__':
134134
test()
135+
136+
137+
######################################### 使用双链表实现 LRUcache ####################################################
138+
"""
139+
一般面试中不会让我们直接用内置结构,所以这里提供一个自己实现的双链表+map lru 缓存。这也是力扣上的一道真题:
140+
[146] LRU 缓存 https://leetcode-cn.com/problems/lru-cache/description/
141+
"""
142+
143+
class ListNode:
144+
def __init__(self, key=None, value=None):
145+
self.key = key
146+
self.value = value
147+
self.prev = self.next = None
148+
149+
150+
class List:
151+
def __init__(self):
152+
"""循环双链表。注意增加了虚拟头尾结点 head,tail 方便处理"""
153+
self.head = ListNode()
154+
self.tail = ListNode()
155+
self.head.prev = self.head.next = self.tail
156+
self.tail.next = self.tail.prev = self.head
157+
158+
def delete_node(self, node): # 删除指定节点
159+
node.prev.next = node.next
160+
node.next.prev = node.prev
161+
162+
def add_to_head(self, node): # 指定节点添加到 self.head 后
163+
nextnode = self.head.next
164+
node.next = nextnode
165+
node.prev = self.head
166+
self.head.next = node
167+
nextnode.prev = node
168+
169+
170+
class LRUCache(object):
171+
172+
def __init__(self, capacity):
173+
"""
174+
思路:循环双链表 + 字典
175+
:type capacity: int
176+
"""
177+
self.map = dict()
178+
self.ll = List()
179+
self.capacity = capacity
180+
181+
def get(self, key):
182+
"""
183+
:type key: int
184+
:rtype: int
185+
"""
186+
if key not in self.map:
187+
return -1
188+
189+
node = self.map[key]
190+
self.ll.delete_node(node)
191+
self.ll.add_to_head(node)
192+
return node.value
193+
194+
def put(self, key, value):
195+
"""
196+
:type key: int
197+
:type value: int
198+
:rtype: None
199+
"""
200+
if key in self.map:
201+
node = self.map[key]
202+
node.value = value # 修改结构体会也会修改 map 对应 value 的引用
203+
self.ll.delete_node(node)
204+
self.ll.add_to_head(node)
205+
else:
206+
if len(self.map) >= self.capacity: # 直接用 len(self.map) ,不需要self.size 字段了
207+
tailnode = self.ll.tail.prev
208+
self.ll.delete_node(tailnode)
209+
del self.map[tailnode.key]
210+
211+
node = ListNode(key, value)
212+
self.map[key] = node
213+
self.ll.add_to_head(node)
214+

19_python内置常用算法和数据结构/builtins/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,22 @@ <h1 id="python-listdict">python list/dict 排序技巧</h1>
361361
# 同时获取最大值的下标和值
362362
l = [1,2,5,4,3]
363363
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
364+
365+
# 获取字典对应的最大值对应的 key,value
366+
mydict = {'A':4,'B':10,'C':0,'D':87}
367+
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
368+
maxk, maxv = maximum, mydict[maximum]
369+
# 或者
370+
maxk, maxv = max(mydict.items(), key=lambda k: k[1])
371+
372+
# python3 排序list自定义函数(python2 直接用 cmp 参数)
373+
from functools import cmp_to_key
374+
nums = [3,2,1,4,5]
375+
sorted(nums, key= cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5]
376+
sorted(nums, key= cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1]
377+
378+
# 一行代码判断列表是否有序
379+
issorted = all(l[i] &lt;= l[i+1] for i in range(len(l) - 1))
364380
</code></pre>
365381
<h1 id="_2">链表题目调试函数</h1>
366382
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
@@ -517,6 +533,7 @@ <h1 id="python_1">python 如何实现最大堆</h1>
517533
</code></pre>
518534
<h1 id="lru_cachecache">lru_cache/cache 优化记忆化搜索</h1>
519535
<p>python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
536+
注意这里的参数 <code>maxsize=None</code> 一定要设置为 None,否则默认的 maxsize=128。
520537
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。</p>
521538
<pre><code class="language-py">&quot;&quot;&quot;
522539
[337] 打家劫舍 III

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,5 +599,5 @@ <h2 id="_22">本电子书制作和写作方式</h2>
599599

600600
<!--
601601
MkDocs version : 1.0.4
602-
Build Date UTC : 2022-03-10 01:16:59
602+
Build Date UTC : 2022-04-07 16:25:19
603603
-->

search/search_index.json

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

sitemap.xml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,122 @@
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
33
<url>
44
<loc>None</loc>
5-
<lastmod>2022-03-10</lastmod>
5+
<lastmod>2022-04-08</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2022-03-10</lastmod>
10+
<lastmod>2022-04-08</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2022-03-10</lastmod>
15+
<lastmod>2022-04-08</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2022-03-10</lastmod>
20+
<lastmod>2022-04-08</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2022-03-10</lastmod>
25+
<lastmod>2022-04-08</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2022-03-10</lastmod>
30+
<lastmod>2022-04-08</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2022-03-10</lastmod>
35+
<lastmod>2022-04-08</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2022-03-10</lastmod>
40+
<lastmod>2022-04-08</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2022-03-10</lastmod>
45+
<lastmod>2022-04-08</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2022-03-10</lastmod>
50+
<lastmod>2022-04-08</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2022-03-10</lastmod>
55+
<lastmod>2022-04-08</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2022-03-10</lastmod>
60+
<lastmod>2022-04-08</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2022-03-10</lastmod>
65+
<lastmod>2022-04-08</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2022-03-10</lastmod>
70+
<lastmod>2022-04-08</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2022-03-10</lastmod>
75+
<lastmod>2022-04-08</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2022-03-10</lastmod>
80+
<lastmod>2022-04-08</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2022-03-10</lastmod>
85+
<lastmod>2022-04-08</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2022-03-10</lastmod>
90+
<lastmod>2022-04-08</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2022-03-10</lastmod>
95+
<lastmod>2022-04-08</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2022-03-10</lastmod>
100+
<lastmod>2022-04-08</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2022-03-10</lastmod>
105+
<lastmod>2022-04-08</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2022-03-10</lastmod>
110+
<lastmod>2022-04-08</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2022-03-10</lastmod>
115+
<lastmod>2022-04-08</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2022-03-10</lastmod>
120+
<lastmod>2022-04-08</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)