leetcode679. 24 点游戏

96 篇文章 1 订阅
81 篇文章 1 订阅

题目就不抄录了,毕竟24点游戏大家都玩过,就是利用加减乘除算出24
遥想刚学java没多久,我就自己写了一个方法来算24,但是当时因为没有leetcode,所以也不知道是不是正确的
然后我这次的方法和当时一样(看来没有进步啊),把我能想到的计算24的公式都写了一遍(经过两次提交失败,把遗漏的也补充上了)
这种方式因为都考虑到了,所以运行时间很快2ms

看了其他答案,不出所料,有把所有枚举都列出来的,但是有个答案让我惊讶,居然没有像我这么死算,也没有只是列举。
看懂意思之后,豁然开朗,我也自己试试。其实思路挺简单的,无论多少数字,我只计算两个,也就加减乘除,然后原本n个数字就变成n-1个数字了,当n成为1时,直接判断是否为24就行

新的代码:

public boolean judgePoint24(int[] nums) {
    double[] ans = new double[4];
    for (int i = 0; i < 4; i++) {
        ans[i] = (double) nums[i];
    }
    return check(ans, 4);
}

private boolean check(double[] ans, int n) {
    if (n == 1) {
        return near(ans[0]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) {
                continue;
            }
            double[] nans = new double[n - 1];
            int key = 1;
            for (int k = 0; k < n; k++) {
                if (k == i || k == j) {
                    continue;
                }
                nans[key++] = ans[k];
            }
            nans[0] = ans[i] * ans[j];
            if (check(nans, n - 1)) {
                return true;
            }
            nans[0] = ans[i] + ans[j];
            if (check(nans, n - 1)) {
                return true;
            }
            nans[0] = ans[i] - ans[j];
            if (check(nans, n - 1)) {
                return true;
            }
            if (!near(ans[j] + 24)) {
                nans[0] = ans[i] / ans[j];
                if (check(nans, n - 1)) {
                    return true;
                }
            }
        }
    }
    return false;
}

private boolean near(double v) {
    return Math.abs(v - 24) < 0.0001;
}

老的代码:

public boolean judgePoint24(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
        for (int j = 0; j < nums.length; j++) {
            if (j == i) {
                continue;
            }
            for (int k = 0; k < nums.length; k++) {
                if (k == i || k == j) {
                    continue;
                }
                double a = nums[i] + 0.0;
                double b = nums[j] + 0.0;
                double c = nums[k] + 0.0;
                double d = nums[6 - i - j - k] + 0.0;
                if (check(a, b, c, d)) {
                    return true;
                }
            }
        }
    }
    return false;
}

private boolean check(double a, double b, double c, double d) {
    if (near(a * b * c * d)) {
        return true;
    }
    if (near(a + b + c + d)) {
        return true;
    }
    if (near(a * b * c + d)) {
        return true;
    }
    if (near(a * b + c * d)) {
        return true;
    }
    if (near(a * b + c + d)) {
        return true;
    }
    if (near((a + b) * (c + d))) {
        return true;
    }
    if (near((a - b) * (c - d))) {
        return true;
    }
    if (near(a * b * c / d)) {
        return true;
    }
    if (near((a * b + c) / d)) {
        return true;
    }
    if (near((a * b - c) / d)) {
        return true;
    }
    if (near(a * b / c + d)) {
        return true;
    }
    if (near(a * b / c - d)) {
        return true;
    }
    if (near((a + b) * (c - d))) {
        return true;
    }
    if (near((a + b / c) * d)) {
        return true;
    }
    if (near((a - b / c) * d)) {
        return true;
    }
    if (near((a / b - c) * d)) {
        return true;
    }
    if (near(a / (b - c / d))) {
        return true;
    }
    if (near(a / (b / c - d))) {
        return true;
    }
    if (near(a * b - c * d)) {
        return true;
    }
    if (near(a * b * c - d)) {
        return true;
    }
    if (near(a * b - c - d)) {
        return true;
    }
    if (near((a - b) * c * d)) {
        return true;
    }
    if (near((a + b) * c * d)) {
        return true;
    }
    if (near((a - b) * c / d)) {
        return true;
    }
    if (near((a + b) * c / d)) {
        return true;
    }
    if (near((a + b) * c + d)) {
        return true;
    }
    if (near((a + b) * c - d)) {
        return true;
    }
    if (near((a - b) * c + d)) {
        return true;
    }
    if (near((a - b) * c - d)) {
        return true;
    }
    if (near(a * b + c - d)) {
        return true;
    }
    if (near(a * b / (c - d))) {
        return true;
    }
    if (near(a * b / (c + d))) {
        return true;
    }
    return false;
}

private boolean near(double v) {
    return Math.abs(v - 24) < 0.001;
}

leetcode的一些已经写的觉得有意思的其他题目
https://blog.csdn.net/qq_33321609/article/category/9012437
如果有我没有写博客的其他题目需要讨论,欢迎评论,一起探讨

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值