Skip to content

Commit 9717c49

Browse files
committed
update
1 parent 4ee7d56 commit 9717c49

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

code/241.为运算表达式设计优先级.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
#
44
# [241] 为运算表达式设计优先级
55
#
6-
6+
from functools import cache
77
# @lc code=start
88
class Solution:
99

10+
@cache
1011
def diffWaysToCompute_(self, numbers, symbols):
1112
if len(symbols) == 0:
1213
return numbers
1314
res = []
1415
for i in range(1, len(numbers)):
16+
17+
left = self.diffWaysToCompute_(numbers[:i], symbols[:i-1])
18+
right = self.diffWaysToCompute_(numbers[i:], symbols[i:])
1519
symbol = symbols[i - 1]
16-
if symbol == '+':
17-
number = numbers[i] + numbers[i - 1]
18-
elif symbol == '-':
19-
number = numbers[i - 1] - numbers[i]
20-
else:
21-
number = numbers[i] * numbers[i - 1]
2220

23-
res.extend(self.diffWaysToCompute_([*numbers[:i-1], number, *numbers[i+1:]], [*symbols[:i - 1], *symbols[i:]]))
21+
for l in left:
22+
for r in right:
23+
24+
if symbol == '+':
25+
res.append(l + r)
26+
elif symbol == '-':
27+
res.append(l - r)
28+
else:
29+
res.append(l * r)
2430
return res
2531

2632

@@ -36,6 +42,5 @@ def diffWaysToCompute(self, expression: str) -> List[int]:
3642
number = ''
3743
symbols.append(exp)
3844
numbers.append(int(number))
39-
return self.diffWaysToCompute_(numbers, symbols)
40-
45+
return self.diffWaysToCompute_(tuple(numbers), tuple(symbols))
4146
# @lc code=end

code/300.最长递增子序列.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# @lc app=leetcode.cn id=300 lang=python3
3+
#
4+
# [300] 最长递增子序列
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def lengthOfLIS(self, nums: List[int]) -> int:
10+
max_ = 0
11+
max_dict = {}
12+
for n in nums:
13+
m = 1
14+
for i in range(max_, 0, -1):
15+
x = max_dict.get(i, None)
16+
if x is not None and n > x:
17+
m = i + 1
18+
if m != 1:
19+
break
20+
x = max_dict.get(m, None)
21+
if x is None or x > n:
22+
max_dict[m] = n
23+
24+
if m > max_:
25+
max_ = m
26+
return max_
27+
# @lc code=end
28+

0 commit comments

Comments
 (0)