[LeetCode/LintCode] 24 Game

 约 8 分钟

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<Double> res01 = calcTwo(nums[0], nums[1]);
        List<Double> res23 = calcTwo(nums[2], nums[3]);
        for (Double i: res01) {
            for (Double j: res23) {
                List<Double> temp = calcTwo(i, j);
                for (Double k: temp) {
                    if (Math.abs(k-24) < 0.001) return true;
                }
            }
        }
        List<Double> res02 = calcTwo(nums[0], nums[2]);
        List<Double> res13 = calcTwo(nums[1], nums[3]);
        for (Double i: res02) {
            for (Double j: res13) {
                List<Double> temp = calcTwo(i, j);
                for (Double k: temp) {
                    if (Math.abs(k-24) < 0.001) return true;
                }
            }
        }
        List<Double> res03 = calcTwo(nums[0], nums[3]);
        List<Double> res12 = calcTwo(nums[1], nums[2]);
        for (Double i: res03) {
            for (Double j: res12) {
                List<Double> 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<Double> res0 = calcThree(nums[3], nums[1], nums[2]);
        List<Double> res1 = calcThree(nums[3], nums[0], nums[2]);
        List<Double> res2 = calcThree(nums[3], nums[0], nums[1]);
        List<Double> res3 = calcThree(nums[1], nums[0], nums[2]);
        for (Double i: res0) {
            List<Double> temp = calcTwo(i, nums[0]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res1) {
            List<Double> temp = calcTwo(i, nums[1]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res2) {
            List<Double> temp = calcTwo(i, nums[2]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        for (Double i: res3) {
            List<Double> temp = calcTwo(i, nums[3]);
            for (Double k: temp) {
                if (Math.abs(k-24) < 0.001) return true;
            }
        }
        return false;
    }
    public List<Double> calcTwo(double a, double b) {
        List<Double> 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<Double> calcThree(double a, double b, double c) {
        List<Double> resAB = new ArrayList<>();
        List<Double> resAC = new ArrayList<>();
        List<Double> resBC = new ArrayList<>();
        resAB.addAll(calcTwo(a, b));
        resAC.addAll(calcTwo(a, c));
        resBC.addAll(calcTwo(b, c));
        List<Double> 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;
    }
}
阅读 884

推荐阅读
Road to Glory
用户专栏

對酒當歌,人生幾何?譬如朝露,去日苦多。

22 人关注
639 篇文章
专栏主页
目录