[679. 24 Game] C++ Recursive
Search for all possible cases.\nLooks like backtracking...\n\n    class Solution {\n    public:\n    double elipson = pow(10.0, -5);\n    vector operations = {'
Search for all possible cases.\nLooks like backtracking...\n\n    class Solution {\n    public:\n    double elipson = pow(10.0, -5);\n    vector<char> operations = {'+','-','*','/'};\n    bool judgePoint24(vector<int>& nums) {\n        vector<double> vec;\n        for(auto n : nums){\n            vec.push_back(n*1.0);\n        }\n        return find24(vec);\n    }\n    \n    bool find24(vector<double> vec){\n        if(vec.size() == 1){\n            return abs(vec[0] - 24.0) <= elipson;\n        }\n        for(int i = 0; i < vec.size(); ++i){//each time we pick up two number for computation\n            for(int j = 0; j < vec.size(); ++j){\n                if(i == j) continue;\n                vector<double> res;\n                for(int k = 0; k < vec.size(); ++k){\n                    if(k != i && k != j) res.push_back(vec[k]);\n                }\n                for(auto op : operations){\n                    if((op == '+' || op == '*') && i > j) continue;//no need to re-calculate\n                    if(op =='/'  && vec[j] == 0.0) continue;\n                    switch(op){\n                        case '+': res.push_back(vec[i] + vec[j]); break;\n                        case '-': res.push_back(vec[i] - vec[j]); break;\n                        case '*': res.push_back(vec[i] * vec[j]); break;\n                        case '/': res.push_back(vec[i] / vec[j]); break;\n                    }\n                    if(find24(res)) return true;\n                    res.pop_back();//!!!important\n                }\n            }\n        }\n        return false;\n    }\n    };
[]