-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffWaysToCompute.py
More file actions
30 lines (24 loc) · 914 Bytes
/
Copy pathdiffWaysToCompute.py
File metadata and controls
30 lines (24 loc) · 914 Bytes
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
# LeetCode
# diffWaysToCompute
# Created by Yigang Zhou on 2020/10/19.
# Copyright © 2020 Yigang Zhou. All rights reserved.
from typing import List
# 241. 为运算表达式设计优先级
# https://leetcode-cn.com/problems/different-ways-to-add-parentheses/
class Solution:
def diffWaysToCompute(self, input: str) -> List[int]:
if input.isdigit():
return [int(input)]
oprands = ['+','-','*']
res = []
for i, each in enumerate(input):
if each in oprands:
left_combinations = self.diffWaysToCompute(input[:i])
right_combinations = self.diffWaysToCompute(input[i+1:])
for l in left_combinations:
for r in right_combinations:
res.append(eval('{}{}{}'.format(l, each, r)))
return res
input = "2*3-4*5"
s = Solution().diffWaysToCompute(input)
print(s)