241. 为运算表达式设计优先级
为保证权益,题目请参考 241. 为运算表达式设计优先级(From LeetCode).
解决方案1
Python
python
# 241. 为运算表达式设计优先级
# https://leetcode.cn/problems/different-ways-to-add-parentheses/
from typing import List
from functools import lru_cache
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
expressions = []
i = 0
while i < len(expression):
if expression[i].isdigit():
t = int(expression[i])
i = i + 1
while i < len(expression) and expression[i].isdigit():
t = t * 10 + int(expression[i])
i = i + 1
expressions.append(t)
else:
expressions.append(expression[i])
i = i + 1
@lru_cache(2 ** len(expressions))
def dfs(l: int, r: int) -> List[int]:
if l > r:
return []
if l == r:
return [expressions[l]]
ans = []
for i in range(l + 1, r, 2):
left = dfs(l, i - 1)
right = dfs(i + 1, r)
if expressions[i] == "-":
for left_v in left:
for right_v in right:
ans.append(left_v - right_v)
elif expressions[i] == "+":
for left_v in left:
for right_v in right:
ans.append(left_v + right_v)
elif expressions[i] == "*":
for left_v in left:
for right_v in right:
ans.append(left_v * right_v)
return ans
return dfs(0, len(expressions) - 1)
if __name__ == "__main__":
so = Solution()
print(so.diffWaysToCompute("2-1-1"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57