679. 24 Game

作者: ShutLove | 来源:发表于2018-03-20 20:23 被阅读18次

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

思路:
开始自己的思路是遍历每个数字,然后考虑数字后面是否加符号,是否能加括号,最后再对搜索完的算术表达式求解,这个思路有两个地方都卡住了,一个是何时能加括号,一个是对最后表达式求解的问题。
最后看答案,豁然开朗,无论怎么加括号,最后的计算结果都是两个数的运算结果,而最后两个数的其中一个数是由前三个数运算而来,由此可以层层向上反推,即先求解任意两个数所有可能的运算结果,然后把它和剩下的两个数组合,递归求解下去,这样的过程就包含了括号可能导致的各种运算顺序情况。

private double score = 24;
    private double esp = 0.001;

    public boolean judgePoint24(int[] nums) {
        if (nums == null || nums.length != 4) {
            return false;
        }

        List<Double> dnums = new ArrayList<>();
        for (int num : nums) {
            dnums.add((double)num);
        }

        return helper(dnums);
    }

    private boolean helper(List<Double> dnums) {
        if (dnums.size() == 1 && Math.abs(dnums.get(0) - score) < esp) {
            return true;
        }

        for (int i = 0; i < dnums.size(); i++) {
            for (int j = i + 1; j < dnums.size(); j++) {
                List<Double> next = new ArrayList<>();
                double n1 = dnums.get(i), n2 = dnums.get(j);
                next.addAll(Arrays.asList(n1+n2, n1-n2, n2-n1, n1*n2));
                if (Math.abs(n1) > esp) {
                    next.add(n2/n1);
                }
                if (Math.abs(n2) > esp) {
                    next.add(n1/n2);
                }

                dnums.remove(i);
                dnums.remove(j);
                for (double tmp : next) {
                    dnums.add(tmp);
                    if (helper(dnums)) {
                        return true;
                    }
                    dnums.remove(tmp);
                }
                dnums.add(j, n2);
                dnums.add(i, n1);
            }
        }
        return false;
    }

相关文章

  • 679. 24 Game

    You have 4 cards each containing a number from 1 to 9. Yo...

  • 679. 24 Game

    You have 4 cards each containing a number from 1 to 9. Yo...

  • 679. 24 点游戏

    679. 24 点游戏 你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 *,/,+,-,(,) 的运...

  • LeetCode 679. 24 Games

    这道题体面要求是四个数,填入+、-、*、/和(),来使其结果为24,实际上可以看成一个组合问题,比如从四个数中任意...

  • Swift编写两款双人小游戏

    Game使用Swift来实现的两个款小游戏,一款是模仿24点游戏,一款是模仿见缝插针 Game1 -- 24 Ga...

  • 译文:手游设计之数值,操作和运气

    原文链接:https://mobilefreetoplay.com/2015/02/24/mobile-game-...

  • 24点python算法实现

    主要参考的https://rosettacode.org/wiki/24_game/Solve对算法本身不是非常理...

  • Leetcode:No.679 24 Game

    You have 4 cards each containing a number from 1 to 9. Yo...

  • Master Of March

    Puzzle Game Game introduction: The game has three roles, ...

  • game game!

    今天过得还算平淡且开心吧。主要开心的就是刚刚玩了无期迷途嘿嘿。它实在是我这些年来,让我体验很好的一款游戏了。我太开...

网友评论

    本文标题:679. 24 Game

    本文链接:https://www.haomeiwen.com/subject/muqiqftx.html