Skip to content

Commit 7f75ad3

Browse files
committed
feat: using cache instead of lru_cache in python solutions
cache and lru_cache(maxsize=None) are exactly the same (linkto cpython source). If we don't want to limit the cache size, using cache may make the code clearer, since a least recently used cache without limit doesn't make much sense.
1 parent 273f37f commit 7f75ad3

File tree

38 files changed

+38
-49
lines changed

38 files changed

+38
-49
lines changed

lcof/面试题43. 1~n整数中1出现的次数/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ high=3,lows=356,base=1000。此时 n 可拆分为 `0~999`,`1000~1999`,`2000~2999
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
from functools import lru_cache
63-
6462
class Solution:
65-
@lru_cache
63+
@cache
6664
def countDigitOne(self, n: int) -> int:
6765
if n < 1:
6866
return 0

lcof/面试题43. 1~n整数中1出现的次数/Solution.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from functools import lru_cache
2-
3-
41
class Solution:
5-
@lru_cache
2+
@cache
63
def countDigitOne(self, n: int) -> int:
74
if n < 1:
85
return 0

lcof2/剑指 Offer II 096. 字符串交织/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution:
7575
if m + n != len(s3):
7676
return False
7777

78-
@lru_cache
78+
@cache
7979
def dfs(i, j):
8080
if i == m and j == n:
8181
return True

lcof2/剑指 Offer II 096. 字符串交织/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
44
if m + n != len(s3):
55
return False
66

7-
@lru_cache
7+
@cache
88
def dfs(i, j):
99
if i == m and j == n:
1010
return True

lcof2/剑指 Offer II 101. 分割等和子串/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Solution:
105105
return False
106106
target = s >> 1
107107

108-
@lru_cache(None)
108+
@cache
109109
def dfs(i, s):
110110
nonlocal target
111111
if s > target or i >= len(nums):

lcof2/剑指 Offer II 102. 加减的目标值/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Solution:
125125
```python
126126
class Solution:
127127
def findTargetSumWays(self, nums: List[int], target: int) -> int:
128-
@lru_cache(None)
128+
@cache
129129
def dfs(i, t):
130130
if i == n:
131131
if t == target:

lcof2/剑指 Offer II 112. 最长递增路径/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
```python
6767
class Solution:
6868
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
69-
@lru_cache(None)
69+
@cache
7070
def dfs(i, j):
7171
ans = 1
7272
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

lcof2/剑指 Offer II 112. 最长递增路径/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i, j):
55
ans = 1
66
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

solution/0000-0099/0097.Interleaving String/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution:
7575
if m + n != len(s3):
7676
return False
7777

78-
@lru_cache
78+
@cache
7979
def dfs(i, j):
8080
if i == m and j == n:
8181
return True

solution/0000-0099/0097.Interleaving String/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Solution:
6464
if m + n != len(s3):
6565
return False
6666

67-
@lru_cache
67+
@cache
6868
def dfs(i, j):
6969
if i == m and j == n:
7070
return True

0 commit comments

Comments
 (0)