C++, Concise code


  • 12
    Z
    class Solution {
    public:
        bool judgePoint24(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            do {
                if (valid(nums)) return true;
            } while(next_permutation(nums.begin(), nums.end()));
            return false;
        }
    private:
        bool valid(vector<int>& nums) {
            double a = nums[0], b = nums[1], c = nums[2], d = nums[3];
            if (valid(a+b, c, d) || valid(a-b, c, d) || valid(a*b, c, d) || valid(a/b, c, d)) return true;
            if (valid(a, b+c, d) || valid(a, b-c, d) || valid(a, b*c, d) || valid(a, b/c, d)) return true;
            if (valid(a, b, c+d) || valid(a, b, c-d) || valid(a, b, c*d) || valid(a, b, c/d)) return true;
            return false;
        }
        bool valid(double a, double b, double c) {
            if (valid(a+b, c) || valid(a-b, c) || valid(a*b, c) || b&&valid(a/b, c)) return true;
            if (valid(a, b+c) || valid(a, b-c) || valid(a, b*c) || c&&valid(a, b/c)) return true;
            return false;
        }
        bool valid(double a, double b) {
            if (abs(a+b-24.0) < 0.0001 || abs(a-b-24.0) < 0.0001 || abs(a*b-24.0) < 0.0001 || b&&abs(a/b-24.0) < 0.0001) 
                return true;
            return false;
        }
    };
    

  • 0
    E

    @zestypanda said in C++, Concise code:

    class Solution {
    public:
        bool judgePoint24(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            do {
                if (valid(nums)) return true;
            } while(next_permutation(nums.begin(), nums.end()));
            return false;
        }
    private:
        bool valid(vector<int>& nums) {
            double a = nums[0], b = nums[1], c = nums[2], d = nums[3];
            if (valid(a+b, c, d) || valid(a-b, c, d) || valid(a*b, c, d) || valid(a/b, c, d)) return true;
            if (valid(a, b+c, d) || valid(a, b-c, d) || valid(a, b*c, d) || valid(a, b/c, d)) return true;
            if (valid(a, b, c+d) || valid(a, b, c-d) || valid(a, b, c*d) || valid(a, b, c/d)) return true;
            return false;
        }
        bool valid(double a, double b, double c) {
            if (valid(a+b, c) || valid(a-b, c) || valid(a*b, c) || b&&valid(a/b, c)) return true;
            if (valid(a, b+c) || valid(a, b-c) || valid(a, b*c) || c&&valid(a, b/c)) return true;
            return false;
        }
        bool valid(double a, double b) {
            if (abs(a+b-24.0) < 0.0001 || abs(a-b-24.0) < 0.0001 || abs(a*b-24.0) < 0.0001 || b&&abs(a/b-24.0) < 0.0001) 
                return true;
            return false;
        }
    };
    
        if (abs(a+b-24.0) < 0.0001 || abs(a-b-24.0) < 0.0001 || abs(a*b-24.0) < 0.0001 || b&&abs(a/b-24.0) < 0.0001) 
    

    In the above code, could you explain what's special about checking that abs(a+b-24.0) is less than 0.0001? Why not 0.000000001 or some other close to 0 value. Does 4 decimal places have some special meaning that's suffices for this problem?


  • 0
    J

    @zestypanda said in C++, Concise code:

    next_permutation

    Nice solution.


Log in to reply
 

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