c++ solution

  • 0

    Further interests you could read this

    class Solution {
        vector<int> diffWaysToCompute(string input) {
            vector<int> result;
            for(int i=0;i<input.size();i++){
                if(input[i]=='+' || input[i]=='-' || input[i]=='*'){
                    for( auto a:diffWaysToCompute(input.substr(0,i)) )
                        for( auto b:diffWaysToCompute(input.substr(i+1)) ){
                            if(input[i]=='+') result.push_back(a+b);
                            if(input[i]=='-') result.push_back(a-b);
                            if(input[i]=='*') result.push_back(a*b);
            if(!result.size()) return {stoi(input)};
            return result;

Log in to reply

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