188188 < li class ="toctree-l2 "> < a href ="#python_1 "> python 如何实现最大堆</ a > </ li >
189189
190190
191+ < li class ="toctree-l2 "> < a href ="#lru_cachecache "> lru_cache/cache 优化记忆化搜索</ a > </ li >
192+
193+
191194 < li class ="toctree-l2 "> < a href ="#leetcode "> leetcode 二叉树调试函数</ a > </ li >
192195
193196
194197 < li class ="toctree-l2 "> < a href ="#python_2 "> python 交换列表元素的坑</ a > </ li >
195198
196199
197- < li class ="toctree-l2 "> < a href ="#_4 "> 兼容提交格式 </ a > </ li >
200+ < li class ="toctree-l2 "> < a href ="#_4 "> 兼容代码提交格式 </ a > </ li >
198201
199202
200203 </ ul >
@@ -499,19 +502,16 @@ <h1 id="python_1">python 如何实现最大堆</h1>
499502
500503def maxheapval(h):
501504 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 "> """
511510[337] 打家劫舍 III
512511https://leetcode-cn.com/problems/house-robber-iii/description/
513512"""
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
515515
516516
517517class Solution(object):
@@ -521,7 +521,7 @@ <h1 id="python_1">python 如何实现最大堆</h1>
521521 :type root: TreeNode
522522 :rtype: int
523523 """
524- # @lru_cache(maxsize=None)
524+ # @lru_cache(maxsize=None) # 注意如果 python3 版本不是很新的话,只能用 lru_cache(maxsize=None)
525525 @cache # NOTE: 不加 cache 会直接超时,就只能用动态规划了
526526 def dfs(root):
527527 if root is None:
@@ -683,10 +683,10 @@ <h1 id="python_2">python 交换列表元素的坑</h1>
683683
684684 return n+1
685685</ code > </ pre >
686- < h1 id ="_4 "> 兼容提交格式 </ h1 >
686+ < h1 id ="_4 "> 兼容代码提交格式 </ h1 >
687687< p > 注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
688688一种是 ACM 模式,还需要自己处理输入和输出。
689- 建议使用这种兼容写法,同样的题目可以同时提交到 牛客和leetcode 。
689+ 建议使用这种兼容写法,同样的题目可以同时提交到 牛客、leetcode 和 acwing(python3) 。
690690这道题目为例子 [679] 奖品分配 https://www.acwing.com/problem/content/681/</ p >
691691< pre > < code class ="language-py "> # 这段代码可以直接以OJ输入模式提交,如果题目一样,直接复制 Solution 类就可以同时提交到leetcode
692692class Solution:
0 commit comments