You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24.

You are restricted with the following rules:


	The division operator '/' represents real division, not integer division.

	
		For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
	
	
	Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
	
		For example, if cards = [1, 1, 1, 1], the expression "-1 - 1 - 1 - 1" is not allowed.
	
	
	You cannot concatenate numbers together
	
		For example, if cards = [1, 2, 1, 2], the expression "12 + 12" is not valid.
	
	


Return true if you can get such expression that evaluates to 24, and false otherwise.

&nbsp;
Example 1:

Input: cards = [4,1,8,7]
Output: true
Explanation: (8-4) * (7-1) = 24


Example 2:

Input: cards = [1,2,1,2]
Output: false


&nbsp;
Constraints:


	cards.length == 4
	1 &lt;= cards[i] &lt;= 9


class Solution:
    def judgePoint24(self, cards: List[int]) -> bool:
        return self.allComputeWays(cards, 4, 24)
        
    def allComputeWays(self, nums, l, target):
        if l == 1:
            if abs(nums[0] - target) <= 1e-6:
                return True
            return False
        for first in range(l):
            for second in range(first + 1, l):
                tmp1, tmp2 = nums[first], nums[second]
                nums[second] = nums[l - 1]
                
                nums[first] = tmp1 + tmp2
                if self.allComputeWays(nums, l - 1, target):
                    return True
                nums[first] = tmp1 - tmp2
                if self.allComputeWays(nums, l - 1, target):
                    return True
                nums[first] = tmp2 - tmp1
                if self.allComputeWays(nums, l - 1, target):
                    return True
                nums[first] = tmp1 * tmp2
                if self.allComputeWays(nums, l - 1, target):
                    return True
                if tmp2:
                    nums[first] = tmp1 / tmp2
                    if self.allComputeWays(nums, l - 1, target):
                        return True
                if tmp1:
                    nums[first] = tmp2 / tmp1
                    if self.allComputeWays(nums, l - 1, target):
                        return True
                nums[first], nums[second] = tmp1, tmp2
        return False
// 0 ms. 100%
class Solution {
    private static final double EPS = 1e-6;
    private boolean backtrack(double[] A, int n) {
        if(n == 1) return Math.abs(A[0] - 24) < EPS;
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                double a = A[i], b = A[j];
                A[j] = A[n-1];
                A[i] = a + b;
                if(backtrack(A, n - 1)) return true;
                A[i] = a - b;
                if(backtrack(A, n - 1)) return true;
                A[i] = b - a;
                if(backtrack(A, n - 1)) return true;
                A[i] = a * b;
                if(backtrack(A, n - 1)) return true;
                if(Math.abs(b) > EPS) {
                    A[i] = a / b;
                    if(backtrack(A, n - 1)) return true;
                }
                if(Math.abs(a) > EPS) {
                    A[i] = b / a;
                    if(backtrack(A, n - 1)) return true;
                }
                A[i] = a; A[j] = b;
            }
        }
        return false;
    }
    public boolean judgePoint24(int[] nums) {
        double[] A = new double[nums.length];
        for(int i = 0; i < nums.length; i++) A[i] = nums[i];
        return backtrack(A, A.length);
    }
}
class Solution {
public:
    double cor=0.001;
    void dfs(vector<double> &cards,bool &res){
        if(res==true) return;
        if(cards.size()==1){
            if(abs(cards[0]-24)<cor) res=true;
            return;
        }
        for(int i=0;i<cards.size();i++){
            for(int j=0;j<i;j++){
                double p=cards[i],q=cards[j];
                vector<double> t{p+q,q-p,p-q,p*q};
                if(p>cor) t.push_back(q/p);
                if(q>cor) t.push_back(p/q);
                cards.erase(cards.begin()+i);
                cards.erase(cards.begin()+j);
                for(double d:t){
                    cards.push_back(d);
                    dfs(cards,res);
                    cards.pop_back();
                }
                cards.insert(cards.begin()+j,q);
                cards.insert(cards.begin()+i,p);
            }
        }
    }
    bool judgePoint24(vector<int>& cards) {
        bool res=false;
        vector<double> card (cards.begin(),cards.end());
        dfs(card,res);
        return res;
    }
};
/**
 * @param {number[]} cards
 * @return {boolean}
1487
7-1 8-4
 */
var judgePoint24 = function(cards) {
    let minV = 0.00000001;
    let numL = [];
    cards.forEach(card=>numL.push(card));
    function judge(nums){
        if(nums.length === 1) return Math.abs(nums[0]-24)<=minV;
        else{
            for(let i = 0 ;i<nums.length;i++){
                for(let j = 0 ;j<i;j++){
                    let a = nums[i] ,b = nums[j];
                    let val =[a + b, a - b, b - a, a * b, a / b, b / a];
                    let copy =[...nums];
                    copy.splice(i,1);
                    copy.splice(j,1);
                    for(let v of val){
                        copy.push(v);
                        if (judge(copy)) {
                            return true;
                        }
                        copy.pop();
                    }
                }
            }
            return false;
        }
    }
    return judge(numL);
};
24 Game