|
1 |
| -# Python 常用内置算法和数据结构 |
| 1 | +# Python 刷题常用内置算法和数据结构 |
2 | 2 |
|
3 | 3 | 相信到这里大家对常用的数据结构和算法及其实现都比较熟悉了。
|
4 | 4 | 之前在每章的数据结构和算法中涉及到的章节我都会提到对应的 python 内置模块,一般如果内置的可以满足需求,我们优先使用内置模块,
|
|
22 | 22 | | 二分算法 | | bisect模块 |
|
23 | 23 | | 堆算法 | | heapq模块 |
|
24 | 24 | | 优先级队列 | | queue.PriorityQueue/heapq |
|
25 |
| -| 缓存算法 | | functools.lru_cache(Least Recent Used, python3) | |
| 25 | +| 缓存算法 | | functools.lru_cache(Least Recent Used, python3)/cache | |
26 | 26 |
|
27 | 27 | # 一些坑
|
28 | 28 |
|
|
32 | 32 | - 正确初始化一个二维数组:`dp = [[0 for _ in range(col)] for _ in range(row)]`,不要用 `dp = [[0] * n] * m`, 否则里边都
|
33 | 33 | 引用的同一个 list,修改一个都会变
|
34 | 34 | - python在数值范围建议用:`MAXINT = 2**63-1; MININT = -2**63` 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一
|
35 |
| -- 优先级队列:使用内置的 heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现 |
| 35 | +- 优先级队列:使用内置queue.PriorityQueue or heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现,下边有代码演示 |
36 | 36 | - python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便
|
37 | 37 |
|
| 38 | +# python int 值范围 |
| 39 | + |
| 40 | +``` |
| 41 | +# 乘方 (比较推荐,py2/3 都兼容不容易出错) |
| 42 | +MAXINT = 2**63-1 |
| 43 | +MININT = -2**63 |
| 44 | +
|
| 45 | +# py3 |
| 46 | +import sys |
| 47 | +MAXINT = sys.maxsize |
| 48 | +MININT = -sys.maxsize - 1 |
| 49 | +
|
| 50 | +# py2 |
| 51 | +sys.maxint |
| 52 | +
|
| 53 | +# 位运算 |
| 54 | +MAXINT = (1<<63) - 1 |
| 55 | +MININT = ~MAXINT |
| 56 | +``` |
| 57 | + |
38 | 58 |
|
39 | 59 | # 链表题目调试函数
|
40 | 60 |
|
@@ -155,3 +175,46 @@ def test_heap_item():
|
155 | 175 | while pq:
|
156 | 176 | print(heapq.heappop(pq))
|
157 | 177 | ```
|
| 178 | + |
| 179 | + |
| 180 | +# lru_cache/cache 优化记忆化搜索 |
| 181 | + |
| 182 | +python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。 |
| 183 | +举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。 |
| 184 | + |
| 185 | +```py |
| 186 | +""" |
| 187 | +[337] 打家劫舍 III |
| 188 | +https://leetcode-cn.com/problems/house-robber-iii/description/ |
| 189 | +""" |
| 190 | +from functools import cache, lru_cache # cache 等价于 functools.lru_cache(maxsize=None) |
| 191 | + |
| 192 | + |
| 193 | +class Solution(object): |
| 194 | + def rob(self, root): |
| 195 | + """ |
| 196 | + 思路 1:递归求解(注意不加 cache 会超时!!) |
| 197 | + :type root: TreeNode |
| 198 | + :rtype: int |
| 199 | + """ |
| 200 | + # @lru_cache(maxsize=None) |
| 201 | + @cache # NOTE: 不加 cache 会直接超时,就只能用动态规划了 |
| 202 | + def dfs(root): |
| 203 | + if root is None: |
| 204 | + return 0 |
| 205 | + |
| 206 | + if root.left is None and root.right is None: # 左右孩子都是空 |
| 207 | + return root.val |
| 208 | + # 不偷父节点,考虑偷 root 的左右孩子 |
| 209 | + val1 = dfs(root.left) + dfs(root.right) |
| 210 | + # 偷父节点 |
| 211 | + val2 = root.val |
| 212 | + if root.left: |
| 213 | + val2 += dfs(root.left.left) + dfs(root.left.right) |
| 214 | + if root.right: |
| 215 | + val2 += dfs(root.right.left) + dfs(root.right.right) |
| 216 | + return max(val1, val2) |
| 217 | + |
| 218 | + return dfs(root) |
| 219 | + |
| 220 | +``` |
0 commit comments