Skip to content

Commit 4a80d96

Browse files
committed
Deployed 51a77e6 with MkDocs version: 1.0.4
1 parent e4280a6 commit 4a80d96

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

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

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,24 @@
167167
<a class="current" href="./">19_python内置常用算法和数据结构</a>
168168
<ul class="subnav">
169169

170-
<li class="toctree-l2"><a href="#python">Python 常用内置算法和数据结构</a></li>
170+
<li class="toctree-l2"><a href="#python">Python 刷题常用内置算法和数据结构</a></li>
171171

172172

173173
<li class="toctree-l2"><a href="#_1">一些坑</a></li>
174174

175175

176+
<li class="toctree-l2"><a href="#python-int">python int 值范围</a></li>
177+
178+
176179
<li class="toctree-l2"><a href="#_2">链表题目调试函数</a></li>
177180

178181

179182
<li class="toctree-l2"><a href="#_3">内置库实现优先级队列的三种方式</a></li>
180183

181184

185+
<li class="toctree-l2"><a href="#lru_cachecache">lru_cache/cache 优化记忆化搜索</a></li>
186+
187+
182188
</ul>
183189
</li>
184190

@@ -219,7 +225,7 @@
219225
<div role="main">
220226
<div class="section">
221227

222-
<h1 id="python">Python 常用内置算法和数据结构</h1>
228+
<h1 id="python">Python 刷题常用内置算法和数据结构</h1>
223229
<p>相信到这里大家对常用的数据结构和算法及其实现都比较熟悉了。
224230
之前在每章的数据结构和算法中涉及到的章节我都会提到对应的 python 内置模块,一般如果内置的可以满足需求,我们优先使用内置模块,
225231
因为在性能和容错性方面内置模块要好于我们自己实现(比如有些是 c 实现的)。本章我们不会再对每个模块的原理详细说明,仅列举出一些常见模块供大家参考,
@@ -283,7 +289,7 @@ <h1 id="python">Python 常用内置算法和数据结构</h1>
283289
<tr>
284290
<td>缓存算法</td>
285291
<td></td>
286-
<td>functools.lru_cache(Least Recent Used, python3)</td>
292+
<td>functools.lru_cache(Least Recent Used, python3)/cache</td>
287293
</tr>
288294
</tbody>
289295
</table>
@@ -294,9 +300,26 @@ <h1 id="_1">一些坑</h1>
294300
<li>正确初始化一个二维数组:<code>dp = [[0 for _ in range(col)] for _ in range(row)]</code>,不要用 <code>dp = [[0] * n] * m</code>, 否则里边都
295301
引用的同一个 list,修改一个都会变</li>
296302
<li>python在数值范围建议用:<code>MAXINT = 2**63-1; MININT = -2**63</code> 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一</li>
297-
<li>优先级队列:使用内置的 heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现</li>
303+
<li>优先级队列:使用内置queue.PriorityQueue or heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现,下边有代码演示</li>
298304
<li>python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便</li>
299305
</ul>
306+
<h1 id="python-int">python int 值范围</h1>
307+
<pre><code># 乘方 (比较推荐,py2/3 都兼容不容易出错)
308+
MAXINT = 2**63-1
309+
MININT = -2**63
310+
311+
# py3
312+
import sys
313+
MAXINT = sys.maxsize
314+
MININT = -sys.maxsize - 1
315+
316+
# py2
317+
sys.maxint
318+
319+
# 位运算
320+
MAXINT = (1&lt;&lt;63) - 1
321+
MININT = ~MAXINT
322+
</code></pre>
300323
<h1 id="_2">链表题目调试函数</h1>
301324
<pre><code class="language-py"># 编写链表题目经常用到的一些通用函数和调试函数,定义等,方便代码调试
302325

@@ -409,6 +432,44 @@ <h1 id="_3">内置库实现优先级队列的三种方式</h1>
409432
heapq.heappush(pq, Item('b', 2))
410433
while pq:
411434
print(heapq.heappop(pq))
435+
</code></pre>
436+
<h1 id="lru_cachecache">lru_cache/cache 优化记忆化搜索</h1>
437+
<p>python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
438+
举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。</p>
439+
<pre><code class="language-py">&quot;&quot;&quot;
440+
[337] 打家劫舍 III
441+
https://leetcode-cn.com/problems/house-robber-iii/description/
442+
&quot;&quot;&quot;
443+
from functools import cache, lru_cache # cache 等价于 functools.lru_cache(maxsize=None)
444+
445+
446+
class Solution(object):
447+
def rob(self, root):
448+
&quot;&quot;&quot;
449+
思路 1:递归求解(注意不加 cache 会超时!!)
450+
:type root: TreeNode
451+
:rtype: int
452+
&quot;&quot;&quot;
453+
# @lru_cache(maxsize=None)
454+
@cache # NOTE: 不加 cache 会直接超时,就只能用动态规划了
455+
def dfs(root):
456+
if root is None:
457+
return 0
458+
459+
if root.left is None and root.right is None: # 左右孩子都是空
460+
return root.val
461+
# 不偷父节点,考虑偷 root 的左右孩子
462+
val1 = dfs(root.left) + dfs(root.right)
463+
# 偷父节点
464+
val2 = root.val
465+
if root.left:
466+
val2 += dfs(root.left.left) + dfs(root.left.right)
467+
if root.right:
468+
val2 += dfs(root.right.left) + dfs(root.right.right)
469+
return max(val1, val2)
470+
471+
return dfs(root)
472+
412473
</code></pre>
413474

414475
</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-02-02 08:29:19
602+
Build Date UTC : 2022-02-02 08:39:34
603603
-->

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)