-
Notifications
You must be signed in to change notification settings - Fork 481
/
0241.py
34 lines (29 loc) · 973 Bytes
/
0241.py
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
class Solution:
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
mem = dict()
return self._diffWaysToCompute(input, mem)
def _diffWaysToCompute(self, input, mem):
if input.isdigit():
return [int(input)]
if input in mem:
return mem[input]
res = list()
for i in range(1, len(input)):
if input[i] in "+-*":
left = self._diffWaysToCompute(input[0:i], mem)
right = self._diffWaysToCompute(input[i+1:], mem)
for l in left:
for r in right:
res.append(self._calc(l, input[i], r))
mem[input] = res
return res
def _calc(self, a, op, b):
return {
'+': a + b,
'-': a - b,
'*': a * b
}[op]