188
188
< li class ="toctree-l2 "> < a href ="#python_1 "> python 如何实现最大堆</ a > </ li >
189
189
190
190
191
+ < li class ="toctree-l2 "> < a href ="#lru_cachecache "> lru_cache/cache 优化记忆化搜索</ a > </ li >
192
+
193
+
191
194
< li class ="toctree-l2 "> < a href ="#leetcode "> leetcode 二叉树调试函数</ a > </ li >
192
195
193
196
194
197
< li class ="toctree-l2 "> < a href ="#python_2 "> python 交换列表元素的坑</ a > </ li >
195
198
196
199
197
- < li class ="toctree-l2 "> < a href ="#_4 "> 兼容提交格式 </ a > </ li >
200
+ < li class ="toctree-l2 "> < a href ="#_4 "> 兼容代码提交格式 </ a > </ li >
198
201
199
202
200
203
</ ul >
@@ -499,19 +502,16 @@ <h1 id="python_1">python 如何实现最大堆</h1>
499
502
500
503
def maxheapval(h):
501
504
return -h[0]
502
- ``````
503
-
504
- # lru_cache/cache 优化记忆化搜索
505
-
506
- python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
507
- 举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。
508
-
509
- ```py
510
- """
505
+ </ code > </ pre >
506
+ < h1 id ="lru_cachecache "> lru_cache/cache 优化记忆化搜索</ h1 >
507
+ < p > python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不过更加轻量更快。在记忆化递归搜索的时候很方便。
508
+ 举一个力扣上的例子,如果不加 cache 递归函数因为会大量重复计算直接超时,但是加一个装饰器就可以通过。</ p >
509
+ < pre > < code class ="language-py "> """
511
510
[337] 打家劫舍 III
512
511
https://leetcode-cn.com/problems/house-robber-iii/description/
513
512
"""
514
- from functools import cache, lru_cache # cache 等价于 functools.lru_cache(maxsize=None)
513
+ # cache 等价于 functools.lru_cache(maxsize=None), 不过python3版本低可能没有 cache 只有 lru_cache
514
+ from functools import cache, lru_cache
515
515
516
516
517
517
class Solution(object):
@@ -521,7 +521,7 @@ <h1 id="python_1">python 如何实现最大堆</h1>
521
521
:type root: TreeNode
522
522
:rtype: int
523
523
"""
524
- # @lru_cache(maxsize=None)
524
+ # @lru_cache(maxsize=None) # 注意如果 python3 版本不是很新的话,只能用 lru_cache(maxsize=None)
525
525
@cache # NOTE: 不加 cache 会直接超时,就只能用动态规划了
526
526
def dfs(root):
527
527
if root is None:
@@ -683,10 +683,10 @@ <h1 id="python_2">python 交换列表元素的坑</h1>
683
683
684
684
return n+1
685
685
</ code > </ pre >
686
- < h1 id ="_4 "> 兼容提交格式 </ h1 >
686
+ < h1 id ="_4 "> 兼容代码提交格式 </ h1 >
687
687
< p > 注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
688
688
一种是 ACM 模式,还需要自己处理输入和输出。
689
- 建议使用这种兼容写法,同样的题目可以同时提交到 牛客和leetcode 。
689
+ 建议使用这种兼容写法,同样的题目可以同时提交到 牛客、leetcode 和 acwing(python3) 。
690
690
这道题目为例子 [679] 奖品分配 https://www.acwing.com/problem/content/681/</ p >
691
691
< pre > < code class ="language-py "> # 这段代码可以直接以OJ输入模式提交,如果题目一样,直接复制 Solution 类就可以同时提交到leetcode
692
692
class Solution:
0 commit comments