Skip to content

Commit a7e0de4

Browse files
committed
python3 functools.cache
1 parent 9a2464d commit a7e0de4

File tree

1 file changed

+6
-5
lines changed
  • docs/19_python内置常用算法和数据结构

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def maxheappop(h):
233233

234234
def maxheapval(h):
235235
return -h[0]
236-
``````
236+
```
237237

238238
# lru_cache/cache 优化记忆化搜索
239239

@@ -245,7 +245,8 @@ python3 functools 模块的 cache 功能和 lru_cache(maxsize=None) 一样,不
245245
[337] 打家劫舍 III
246246
https://leetcode-cn.com/problems/house-robber-iii/description/
247247
"""
248-
from functools import cache, lru_cache # cache 等价于 functools.lru_cache(maxsize=None)
248+
# cache 等价于 functools.lru_cache(maxsize=None), 不过python3版本低可能没有 cache 只有 lru_cache
249+
from functools import cache, lru_cache
249250

250251

251252
class Solution(object):
@@ -255,7 +256,7 @@ class Solution(object):
255256
:type root: TreeNode
256257
:rtype: int
257258
"""
258-
# @lru_cache(maxsize=None)
259+
# @lru_cache(maxsize=None) # 注意如果 python3 版本不是很新的话,只能用 lru_cache(maxsize=None)
259260
@cache # NOTE: 不加 cache 会直接超时,就只能用动态规划了
260261
def dfs(root):
261262
if root is None:
@@ -425,11 +426,11 @@ class Solution(object):
425426
return n+1
426427
```
427428

428-
# 兼容提交格式
429+
# 兼容代码提交格式
429430

430431
注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
431432
一种是 ACM 模式,还需要自己处理输入和输出。
432-
建议使用这种兼容写法,同样的题目可以同时提交到 牛客和leetcode
433+
建议使用这种兼容写法,同样的题目可以同时提交到 牛客、leetcode 和 acwing(python3)
433434
这道题目为例子 [679] 奖品分配 https://www.acwing.com/problem/content/681/
434435

435436
```py

0 commit comments

Comments
 (0)