Skip to content

Commit f8b5c6e

Browse files
committed
Deployed cde0457 with MkDocs version: 1.0.4
1 parent 8864ab4 commit f8b5c6e

File tree

7 files changed

+103
-27
lines changed

7 files changed

+103
-27
lines changed

15_堆与堆排序/min_heap.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# python3
2+
class MinHeap:
3+
def __init__(self):
4+
"""
5+
这里提供一个最小堆实现。如果面试不让用内置的堆非让你自己实现的话,考虑用这个简版的最小堆实现。
6+
一般只需要实现 heqppop,heappush 两个操作就可以应付面试题了
7+
parent: (i-1)//2。注意这么写 int((n-1)/2), python3 (n-1)//2当n=0结果是-1而不是0
8+
left: 2*i+1
9+
right: 2*i+2
10+
参考:
11+
https://favtutor.com/blogs/heap-in-python
12+
https://runestone.academy/ns/books/published/pythonds/Trees/BinaryHeapImplementation.html
13+
https://www.askpython.com/python/examples/min-heap
14+
"""
15+
self.pq = []
16+
17+
def min_heapify(self, nums, k):
18+
"""递归调用,维持最小堆特性"""
19+
l = 2*k+1 # 左节点位置
20+
r = 2*k+2 # 右节点
21+
if l < len(nums) and nums[l] < nums[k]:
22+
smallest = l
23+
else:
24+
smallest = k
25+
if r < len(nums) and nums[r] < nums[smallest]:
26+
smallest = r
27+
if smallest != k:
28+
nums[k], nums[smallest] = nums[smallest], nums[k]
29+
self.min_heapify(nums, smallest)
30+
31+
def heappush(self, num):
32+
"""列表最后就加入一个元素,之后不断循环调用维持堆特性"""
33+
self.pq.append(num)
34+
n = len(self.pq) - 1
35+
# 注意必须加上n>0。因为 python3 (n-1)//2 当n==0 的时候结果是-1而不是0!
36+
while n > 0 and self.pq[n] < self.pq[(n-1)//2]: # parent 交换
37+
self.pq[n], self.pq[(n-1)//2] = self.pq[(n-1)//2], self.pq[n] # swap
38+
n = (n-1)//2
39+
40+
def heqppop(self): # 取 pq[0],之后和pq最后一个元素pq[-1]交换之后调用 min_heapify(0)
41+
minval = self.pq[0]
42+
last = self.pq[-1]
43+
self.pq[0] = last
44+
self.min_heapify(self.pq, 0)
45+
self.pq.pop()
46+
return minval
47+
48+
def heapify(self, nums):
49+
n = int((len(nums)//2)-1)
50+
for k in range(n, -1, -1):
51+
self.min_heapify(nums, k)
52+
53+
54+
def test_MinHeqp():
55+
import random
56+
l = list(range(1, 9))
57+
random.shuffle(l)
58+
pq = MinHeap()
59+
for num in l:
60+
pq.heappush(num)
61+
res = []
62+
for _ in range(len(l)):
63+
res.append(pq.heqppop()) # 利用 heqppop,heqppush 实现堆排序
64+
65+
def issorted(l): return all(l[i] <= l[i+1] for i in range(len(l) - 1))
66+
assert issorted(res)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ <h1 id="python_1">python 如何实现最大堆</h1>
534534
<h1 id="lru_cachecache">lru_cache/cache 优化记忆化搜索</h1>
535535
<p>python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
536536
注意这里的参数 <code>maxsize=None</code> 一定要设置为 None,否则默认的 maxsize=128。
537-
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。</p>
537+
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。
538+
当然了如果你用 python2 没有这个装饰器,你可以直接用 python 的 dict 来实现。(存在就返回,否则计算结果保存到 dict 里)</p>
538539
<pre><code class="language-py">&quot;&quot;&quot;
539540
[337] 打家劫舍 III
540541
https://leetcode-cn.com/problems/house-robber-iii/description/

20_面试指南/interview/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@
187187
<li class="toctree-l2"><a href="#_5">延伸阅读</a></li>
188188

189189

190+
<li class="toctree-l2"><a href="#_6">刷题网站</a></li>
191+
192+
190193
</ul>
191194
</li>
192195

@@ -261,6 +264,12 @@ <h1 id="_5">延伸阅读</h1>
261264
<li><a href="https://book.douban.com/subject/25753386/">《程序员面试金典(第5版)》</a></li>
262265
<li><a href="https://book.douban.com/subject/25910559/">《剑指Offer》</a></li>
263266
<li><a href="https://github.com/HuberTRoy/leetCodek">python leetCode</a></li>
267+
</ul>
268+
<h1 id="_6">刷题网站</h1>
269+
<p>leetcode 和牛客网是国内常用的两个刷题网站,笔者建议刷一下高频的 200 道题左右,基本可以应付大部分公司的算法面试了。</p>
270+
<ul>
271+
<li>https://leetcode-cn.com/</li>
272+
<li>https://www.nowcoder.com/exam/oj</li>
264273
</ul>
265274

266275
</div>

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

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)