반응형
leetcode.com/problems/different-ways-to-add-parentheses/
class Solution:
def diffWaysToCompute(self, expression: str) -> List[int]:
values = []
def compute(left,right,op):
for l in left:
for r in right:
values.append(eval(str(l)+op+str(r))
if expression.isdigit():
return [int(expression)]
for i in range(len(expression)):
if expression[i] in "+-*":
left = self.diffWaysToCompute(expression[:i])
right = self.diffWaysToCompute(expression[i+1:])
compute(left,right,expression[i])
return values
댓글