c++ solution of mine


  • 0
    Y

    class Solution {
    unordered_map<char,function<int(int,int)>> op {
    {'+', [](int a, int b){return a+b;}},
    {'-', [](int a, int b){return a-b;}},
    {'', [](int a, int b){return ab;}},
    {'/', [](int a, int b){return a/b;}}
    };

    std::function<int(string)> S2I = [](string s){
        int n;
        stringstream(s) >> n;
        return n;
    };
    

    public:
    vector<int> diffWaysToCompute(string input) {
    if(!std::any_of(input.begin(), input.end(), [](char c){ return c=='+'||c=='-'||c=='*'||c=='/';})) {
    return { S2I(input) };
    }

        vector<int> nums;
    
        for(int i=0; i<input.size(); ++i) {    
            if(input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/') {
                auto leftnums = diffWaysToCompute( input.substr(0,i) );
                auto rightnums = diffWaysToCompute( input.substr(i+1));
                for(auto n: leftnums) {
                    for(auto m: rightnums) {
                        nums.push_back(op[input[i]](n,m));
                    }
                }
            }
        }
        
        return nums;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.