以前写过
完全忘了
今天没什么心情写
直接瞥一下答案
哀
def diffWaysToCompute(self, expression: str) -> List[int]:
ans = []
for i,c in enumerate(expression):
if c!='+' and c!='*' and c!='-':
continue
else:
a = self.diffWaysToCompute(expression[:i])
b = self.diffWaysToCompute(expression[i+1:])
for num1 in a:
for num2 in b:
if c=='+':
ans.append(num1+num2)
elif c=='-':
ans.append(num1-num2)
elif c=='*':
ans.append(num1*num2)
if len(ans)==0 and len(expression)>0:
ans.append(int(expression))
return ans