要求:所有运算组合
思路:分治,而且要循环
class Solution {
public:
vector<int> diffWaysToCompute(string expression) {
int n=expression.length();
vector<int> ans;
for(int i=0;i<n;i++){
char op=expression[i];
if(op=='+'||op=='-'||op=='*'){
vector<int> left=diffWaysToCompute(expression.substr(0,i));
vector<int> right=diffWaysToCompute(expression.substr(i+1));
for(int &l:left){
for(int &r:right){
switch(op){
case '+':ans.push_back(l+r);break;
case '-':ans.push_back(l-r);break;
case '*':ans.push_back(l*r);break;
}
}
}
}
}
if(ans.size()==0)ans.push_back(stoi(expression));
return ans;
}
};