点击打开链接
class Solution { public: vector<int> diffWaysToCompute(string input) { vector<int> result; char c= '\0'; for(int i=0; i< input.size(); i++){ c = input[i]; if(c=='+' || c=='-' || c=='*'){ vector<int> result1 = diffWaysToCompute(input.substr(0, i)); //从index0开始,选取i个字符,到index i-1; vector<int> result2 = diffWaysToCompute(input.substr(i+1)); //从index i+1开始截取 for(int r1=0; r1<result1.size(); r1++){ for(int r2=0; r2<result2.size(); r2++){ if(c=='+'){ result.push_back(result1[r1]+ result2[r2]); } else if(c=='-'){ result.push_back(result1[r1]- result2[r2]); } else if(c=='*'){ result.push_back(result1[r1]*result2[r2]); } } } } } if(result.empty()){//也可以是递归终止点 result.push_back(atoi(input.c_str())); } return result; } };