241. Different Ways to Add Parentheses
class Solution(object):
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
res = []
for i, v in enumerate(input):
if v in "+-*":
for l1 in self.diffWaysToCompute(input[:i]):
for l2 in self.diffWaysToCompute(input[i+1:]):
if v == '+':
res.append(l1 + l2)
elif v == "-":
res.append(l1 - l2)
else:
res.append(l1 * l2)
if len(res) == 0:
return [int(input)]
return res