Thinking Process - Backtracking
Thanks to (, ) operators, we don\'t take order of operations into consideration.\n>\n>Let\'s say, we pick any two numbers, a and b, and apply any operator +, -,
>Thanks to `(, )` operators, we don\'t take order of operations into consideration.\n>\n>Let\'s say, we pick any two numbers, `a` and `b`, and apply any operator `+, -, *, / `, assuming that the expression is surrounded with parenthesis, e.g. `(a + b)`. Then, the result of `(a + b)` instead of `a` and `b` would participate in the following calculations.\nTake `nums = [4, 3, 2, 1]` for example, assuming that we pick`4, 3` and apply `+`, `nums` would become `[7, 2, 1]`.\n\n>We keep enumerating until there is only one element left in `nums`, and return true only if the last element equals to `24`.\n\n>`DIFF_TOLERANT` is established because division between Double data type may cause loss of precision.\n****\n```\nclass Solution {\n    private static final double DIFF_TOLERANT = 0.1;\n    \n    public boolean judgePoint24(int[] nums) {\n        List<Double> numList = new ArrayList<>();\n        for (int num : nums) {\n            numList.add((double)num);\n        }\n        return judgePoint24(numList);\n    }\n    \n    private boolean judgePoint24(List<Double> nums) {\n        if (nums.size() == 1) {\n            return Math.abs(nums.get(0) - 24) <= DIFF_TOLERANT;\n        } else {\n            for (int i = 0; i < nums.size(); i++) {\n                for (int j = 0; j < i; j++) {\n                    double a = nums.get(i), b = nums.get(j);\n                    \n                    List<Double> vals = new ArrayList<>(); // possible results by adding operator between a and b\n                    // invariant: no 0 in nums\n                    vals.addAll(Arrays.asList(a + b, a - b, b - a, a * b, a / b, b / a));\n                    \n                    List<Double> copyNums = new ArrayList<>(nums); // deep copy of nums\n                    copyNums.remove(i);\n                    copyNums.remove(j);\n                    for (double val : vals) {\n                        copyNums.add(val);\n                        if (judgePoint24(copyNums)) {\n                            return true;\n                        }\n                        copyNums.remove(copyNums.size() - 1);\n                    }\n                }\n            }\n            return false;\n        }\n    }\n}\n```\n**\u2727\u207A\u2E1C(\u25CF\u02D9\u25BE\u02D9\u25CF)\u2E1D\u207A\u2727**
[]