[LeetCode/LintCode] 24 Game

Problem

You have 4 cards each containing a number from 1 to 9. You need to compute whether they could operated through *, /, +, -, (, ) to get the value of 24.

Example

Given nums = [4, 1, 8, 7], return true // 8 (7 - 4) 1 = 24

Given nums = [1, 1, 1, 2], return false

Given nums = [3, 3, 8, 8], return true // 8 / ( 3 - 8 / 3) = 24

Solution

public class Solution {
    /**
     * @param nums: 4 cards
     * @return: whether they could get the value of 24
     */
    public boolean compute24(double[] nums) {
        return (calc2vs2(nums)) || (calc1vs3(nums));
    }
    public boolean calc2vs2(double[] nums) {
        List res01 = calcTwo(nums[0], nums[1]);
        List res23 = calcTwo(nums[2], nums[3]);
        for (Double i: res01) {
            for (Double j: res23) {
                List temp = calcTwo(i, j);
                for (Double k: temp) {
                    if (Math.abs(k-24) < 0.001) return true;
                }
            }
        }
        List res02 = calcTwo(nums[0], nums[2]);
        List res13 = calcTwo(nums[1], nums[3]);
        for (Double i: res02) {
            for (Double j: res13) {
                List temp = calcTwo(i, j);
                for (Double k: temp) {
                    if (Math.abs(k-24) < 0.001) return true;
                }
            }
        }
        List res03 = calcTwo(nums[0], nums[3]);
        List res12 = calcTwo(nums[1], nums[2]);
        for (Double i: res03) {
            for (Double j: res12) {
                List temp = calcTwo(i, j);
                for (Double k: temp) {
                    if (Math.abs(k-24) < 0.001) return true;
                }
            }
        }
        return false;
    }
    public boolean calc1vs3(double[] nums) {
        List res0 = calcThree(nums[3], nums[1], nums[2]);
        List res1 = calcThree(nums[3], nums[0], nums[2]);
        List res2 = calcThree(nums[3], nums[0], nums[1]);
        List res3 = calcThree(nums[1], nums[0], nums[2]);
        for (Double i: res0) {
            List temp = calcTwo(i, nums[0]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res1) {
            List temp = calcTwo(i, nums[1]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res2) {
            List temp = calcTwo(i, nums[2]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res3) {
            List temp = calcTwo(i, nums[3]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        return false;
    }
    public List calcTwo(double a, double b) {
        List res = new ArrayList<>();
        res.add(a+b);
        res.add(a-b);
        res.add(b-a);
        res.add(a*b);
        res.add(a/b);
        res.add(b/a);
        return res;
    }
    public List calcThree(double a, double b, double c) {
        List resAB = new ArrayList<>();
        List resAC = new ArrayList<>();
        List resBC = new ArrayList<>();
        resAB.addAll(calcTwo(a, b));
        resAC.addAll(calcTwo(a, c));
        resBC.addAll(calcTwo(b, c));
        List res = new ArrayList<>();
        for (Double i: resAB) {
            res.addAll(calcTwo(i, c));
        }
        for (Double i: resAC) {
            res.addAll(calcTwo(i, b));
        }
        for (Double i: resBC) {
            res.addAll(calcTwo(i, a));
        }
        return res;
    }
}

你可能感兴趣的:(dfs,LintCode,leetcode)