Skip to content

Commit e74aeac

Browse files
committed
Deployed 9a2464d with MkDocs version: 1.0.4
1 parent 029abbc commit e74aeac

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-33
lines changed

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@
185185
<li class="toctree-l2"><a href="#_3">内置库实现优先级队列的三种方式</a></li>
186186

187187

188-
<li class="toctree-l2"><a href="#lru_cachecache">lru_cache/cache 优化记忆化搜索</a></li>
188+
<li class="toctree-l2"><a href="#python_1">python 如何实现最大堆</a></li>
189189

190190

191191
<li class="toctree-l2"><a href="#leetcode">leetcode 二叉树调试函数</a></li>
192192

193193

194-
<li class="toctree-l2"><a href="#python_1">python 交换列表元素的坑</a></li>
194+
<li class="toctree-l2"><a href="#python_2">python 交换列表元素的坑</a></li>
195195

196196

197197
<li class="toctree-l2"><a href="#_4">兼容提交格式</a></li>
@@ -462,10 +462,52 @@ <h1 id="_3">内置库实现优先级队列的三种方式</h1>
462462
while pq:
463463
print(heapq.heappop(pq))
464464
</code></pre>
465-
<h1 id="lru_cachecache">lru_cache/cache 优化记忆化搜索</h1>
466-
<p>python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
467-
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。</p>
468-
<pre><code class="language-py">&quot;&quot;&quot;
465+
<h1 id="python_1">python 如何实现最大堆</h1>
466+
<p>python自带了heapq 模块实现了最小堆(min-heaq),但是如果想要实现最大堆(max-heap),有几种实现方式:</p>
467+
<ol>
468+
<li>对放入的数字取反。比如 10 放入 -10 ,然后取出来的时候再取反。个人倾向于这种,可以自己封装一个类防止来回取反搞晕</li>
469+
<li>直接根据 heapq 模块的函数封装几个最大堆的函数,也是通过取反实现</li>
470+
<li>新建一个对象重写 <code>__lt__</code> 魔术方法。这种方式也可以,但是重写魔术方法修改了语义不太好(个人不推荐)</li>
471+
</ol>
472+
<pre><code class="language-py"># 方法1:封装一个 max heap 类
473+
import heapq
474+
class MaxHeap:
475+
&quot;&quot;&quot;
476+
https://stackoverflow.com/questions/2501457/what-do-i-use-for-a-max-heap-implementation-in-python
477+
&quot;&quot;&quot;
478+
def __init__(self, capacity):
479+
self.capacity = capacity
480+
self.minheap = []
481+
482+
def push(self, val):
483+
heapq.heappush(self.minheap, -val) # push取反后的数字, 1 -&gt; -1
484+
485+
def pop(self):
486+
val = heapq.heappop(self.minheap)
487+
return -val # 拿出来的数字再取反
488+
489+
def max(self):
490+
return -self.minheap[0] # min-heap 的数组最小值是 m[0],最大值取反
491+
492+
# 方法2: 重新定几个新的 max-heap 方法
493+
import heapq
494+
def maxheappush(h, item):
495+
return heapq.heappush(h, -item)
496+
497+
def maxheappop(h):
498+
return -heapq.heappop(h)
499+
500+
def maxheapval(h):
501+
return -h[0]
502+
``````
503+
504+
# lru_cache/cache 优化记忆化搜索
505+
506+
python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
507+
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。
508+
509+
```py
510+
&quot;&quot;&quot;
469511
[337] 打家劫舍 III
470512
https://leetcode-cn.com/problems/house-robber-iii/description/
471513
&quot;&quot;&quot;
@@ -614,7 +656,7 @@ <h1 id="leetcode">leetcode 二叉树调试函数</h1>
614656
cur.right = right
615657
return root
616658
</code></pre>
617-
<h1 id="python_1">python 交换列表元素的坑</h1>
659+
<h1 id="python_2">python 交换列表元素的坑</h1>
618660
<pre><code># 41. 缺失的第一个正数 https://leetcode-cn.com/problems/first-missing-positive/
619661
class Solution(object):
620662
def firstMissingPositive(self, nums):

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-02-19 08:43:39
602+
Build Date UTC : 2022-02-20 03:59:08
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-02-19</lastmod>
5+
<lastmod>2022-02-20</lastmod>
66
<changefreq>daily</changefreq>
77
</url>
88
<url>
99
<loc>None</loc>
10-
<lastmod>2022-02-19</lastmod>
10+
<lastmod>2022-02-20</lastmod>
1111
<changefreq>daily</changefreq>
1212
</url>
1313
<url>
1414
<loc>None</loc>
15-
<lastmod>2022-02-19</lastmod>
15+
<lastmod>2022-02-20</lastmod>
1616
<changefreq>daily</changefreq>
1717
</url>
1818
<url>
1919
<loc>None</loc>
20-
<lastmod>2022-02-19</lastmod>
20+
<lastmod>2022-02-20</lastmod>
2121
<changefreq>daily</changefreq>
2222
</url>
2323
<url>
2424
<loc>None</loc>
25-
<lastmod>2022-02-19</lastmod>
25+
<lastmod>2022-02-20</lastmod>
2626
<changefreq>daily</changefreq>
2727
</url>
2828
<url>
2929
<loc>None</loc>
30-
<lastmod>2022-02-19</lastmod>
30+
<lastmod>2022-02-20</lastmod>
3131
<changefreq>daily</changefreq>
3232
</url>
3333
<url>
3434
<loc>None</loc>
35-
<lastmod>2022-02-19</lastmod>
35+
<lastmod>2022-02-20</lastmod>
3636
<changefreq>daily</changefreq>
3737
</url>
3838
<url>
3939
<loc>None</loc>
40-
<lastmod>2022-02-19</lastmod>
40+
<lastmod>2022-02-20</lastmod>
4141
<changefreq>daily</changefreq>
4242
</url>
4343
<url>
4444
<loc>None</loc>
45-
<lastmod>2022-02-19</lastmod>
45+
<lastmod>2022-02-20</lastmod>
4646
<changefreq>daily</changefreq>
4747
</url>
4848
<url>
4949
<loc>None</loc>
50-
<lastmod>2022-02-19</lastmod>
50+
<lastmod>2022-02-20</lastmod>
5151
<changefreq>daily</changefreq>
5252
</url>
5353
<url>
5454
<loc>None</loc>
55-
<lastmod>2022-02-19</lastmod>
55+
<lastmod>2022-02-20</lastmod>
5656
<changefreq>daily</changefreq>
5757
</url>
5858
<url>
5959
<loc>None</loc>
60-
<lastmod>2022-02-19</lastmod>
60+
<lastmod>2022-02-20</lastmod>
6161
<changefreq>daily</changefreq>
6262
</url>
6363
<url>
6464
<loc>None</loc>
65-
<lastmod>2022-02-19</lastmod>
65+
<lastmod>2022-02-20</lastmod>
6666
<changefreq>daily</changefreq>
6767
</url>
6868
<url>
6969
<loc>None</loc>
70-
<lastmod>2022-02-19</lastmod>
70+
<lastmod>2022-02-20</lastmod>
7171
<changefreq>daily</changefreq>
7272
</url>
7373
<url>
7474
<loc>None</loc>
75-
<lastmod>2022-02-19</lastmod>
75+
<lastmod>2022-02-20</lastmod>
7676
<changefreq>daily</changefreq>
7777
</url>
7878
<url>
7979
<loc>None</loc>
80-
<lastmod>2022-02-19</lastmod>
80+
<lastmod>2022-02-20</lastmod>
8181
<changefreq>daily</changefreq>
8282
</url>
8383
<url>
8484
<loc>None</loc>
85-
<lastmod>2022-02-19</lastmod>
85+
<lastmod>2022-02-20</lastmod>
8686
<changefreq>daily</changefreq>
8787
</url>
8888
<url>
8989
<loc>None</loc>
90-
<lastmod>2022-02-19</lastmod>
90+
<lastmod>2022-02-20</lastmod>
9191
<changefreq>daily</changefreq>
9292
</url>
9393
<url>
9494
<loc>None</loc>
95-
<lastmod>2022-02-19</lastmod>
95+
<lastmod>2022-02-20</lastmod>
9696
<changefreq>daily</changefreq>
9797
</url>
9898
<url>
9999
<loc>None</loc>
100-
<lastmod>2022-02-19</lastmod>
100+
<lastmod>2022-02-20</lastmod>
101101
<changefreq>daily</changefreq>
102102
</url>
103103
<url>
104104
<loc>None</loc>
105-
<lastmod>2022-02-19</lastmod>
105+
<lastmod>2022-02-20</lastmod>
106106
<changefreq>daily</changefreq>
107107
</url>
108108
<url>
109109
<loc>None</loc>
110-
<lastmod>2022-02-19</lastmod>
110+
<lastmod>2022-02-20</lastmod>
111111
<changefreq>daily</changefreq>
112112
</url>
113113
<url>
114114
<loc>None</loc>
115-
<lastmod>2022-02-19</lastmod>
115+
<lastmod>2022-02-20</lastmod>
116116
<changefreq>daily</changefreq>
117117
</url>
118118
<url>
119119
<loc>None</loc>
120-
<lastmod>2022-02-19</lastmod>
120+
<lastmod>2022-02-20</lastmod>
121121
<changefreq>daily</changefreq>
122122
</url>
123123
</urlset>

sitemap.xml.gz

-1 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)