其他分享
首页 > 其他分享> > 241. 为运算表达式设计优先级(分治)

241. 为运算表达式设计优先级(分治)

作者:互联网

class Solution {
public:
    vector<int> diffWaysToCompute(string input) {
        vector<int> res;
        int n = input.size();
        for(int i = 0; i < n; i++) {
            char c = input[i];
            if(c < '0' || c > '9') {
                vector<int> left = diffWaysToCompute(input.substr(0,i));
                vector<int> right = diffWaysToCompute(input.substr(i+1,n));
                for(int l : left) {
                    for(int r : right) {
                        if(c == '+') {
                            res.push_back(l + r);
                        } else if (c == '-') {
                            res.push_back(l - r);
                        } else {
                            res.push_back(l * r);
                        }
                    }
                }
            }
        }
        if(!res.size()) res.push_back(stoi(input));
        return res;
    }
};

 

标签:优先级,int,res,分治,back,vector,241,push,input
来源: https://www.cnblogs.com/yonezu/p/13579840.html