传送门

Description

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.

Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False

Note:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 -1 -1 -1 is not allowed.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2],  we cannot write this as 12 + 12.

思路

题意:给定四张标有数字(1-9)的卡片,问通过加减乘除和小括号组成的表达式的结果能否等于24。

题解:考虑穷举法的话,四个数字共有4!种排列方法,4个数字中间加入符号共有4x4x4种方法,最后考虑小括号,小括号的放法共有(A(B(CD))、(A((BC)D)、((AB)(CD))、((A(BC))D)、(((AB)C)D)五种,那么种类最多有4!x4^3x5 = 7680种。

考虑回溯法,首先我们从集合A = {1 、2、3、4}中任意取出两个数,如取1、2,那么A = A - {1、2},对取出来的两个数字分别进行不同的四则运算,1 + 2、1 - 2……,将结果加入A中,有{3、3、4}、{-1,3,4}等,通过这种方法,将四个数降为三个数,然后降为两个数……

Java

class Solution {
    boolean res = false;
    final double esp = 1e-4;
    public boolean judgePoint24(int[] nums) {
        List<Double>list = new ArrayList<Double>();
        for (int val:nums)  list.add((double)val);
        solver(list);
        return res;
    }

    void solver(List<Double> array){
        if (res)    return;
        if (array.size() == 1 && Math.abs(array.get(0) - 24.0) <= esp){
            res = true;
            return;
        }

        for (int i = 0;i < array.size();i++){
            for (int j = 0;j < i;j++){
                List<Double>list = new ArrayList<Double>();
                Double p1 = array.get(i),p2 = array.get(j);
                list.addAll(Arrays.asList(p1+p2,p1-p2,p2-p1,p1*p2));

                //除数是否为0
                if (Math.abs(p1) > esp){
                    list.add(p2/p1);
                }
                if (Math.abs(p2) > esp){
                    list.add(p1/p2);
                }

                array.remove(i);
                array.remove(j);

                for (Double val:list){
                    array.add(val);
                    solver(array);
                    array.remove(val);
                }

                array.add(j,p2);
                array.add(i,p1);

            }
        }
    }
}

  

[LeetCode] 679. 24 Game(回溯法)的更多相关文章

  1. Leetcode 679.24点游戏

    24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) ...

  2. LeetCode刷题笔记-回溯法-括号生成

    题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "( ...

  3. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

  4. Java实现 LeetCode 679 24 点游戏(递归)

    679. 24 点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: ...

  5. [leetcode]79.Search Word 回溯法

    /** * Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  6. 每日一题 LeetCode 679. 24点游戏 【递归】【全排列】

    题目链接 https://leetcode-cn.com/problems/24-game/ 题目说明 题解 主要方法:递归 + 全排列 解释说明: 将 4 个数进行组合形成算式,发现除了 (a❈b) ...

  7. [leetcode] 679. 24 Game (Hard)

    24点游戏,游戏规则就是利用().+.-. *. /,对四个数字任意运算,可以得出24点则为true. 排列组合问题,最多有A42*A32*A22*4*4*4,也就是12*6*2*4*4=9216种组 ...

  8. LeetCode刷题笔记-回溯法-组合总和问题

    题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...

  9. Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)

    Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...

随机推荐

  1. HDU 1562 Oil Deposits

    题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  2. 用PHP,怎么获取PHP.ini中的文件上传最大的字节数。也就是默认的2M

    PHP中用ini_get函数来获取服务器允许的文件上传最大字节数,如:

  3. 织梦CMS搭建网站必做的服务器相关安全设置

    http://help.dedecms.com/install-use/server/2011/1109/2124.html#printSource http://www.aliweihu.com/9 ...

  4. SendCloud邮件中为什么会显示代发

    显示代发是发信的sender和发信人地址(from)不一致导致的. sender是SendCloud系统根据用户自设的发信域名生成的,目前是"随机地址@自有域名"做sender,所 ...

  5. MYSQL Optimizing LIMIT Queries

    如果要指定查询的数据行数,在查询语句中使用limit子句,而不是获取所有数据行结果集,然后去掉没用的数据. MYSQL有时会对没有having子句,带有limit关键字的查询进行优化: 1:如果用li ...

  6. C# 内置 DateTime类详解

    C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...

  7. linux 巨页使用测试以及勘误1

    linux使用hugetlbfs的方式来支持巨页,也成为大页. 网上看到有人说巨页不支持read,和write调用,只支持mmap,但是看3.10内核代码的时候发现: const struct fil ...

  8. intellij-项目目录隐藏无用的文件和文件夹

    File-->Editor-->File Types

  9. float是什么样式?

    什么是float样式? 让标签浮动起来,总体方向往上 right,left(右浮,左浮) 联合height,width使用,分别占用y方向和x方向多少,单位px或百分比(%) 作用对象不是页面,而是作 ...

  10. python_如何为元组中每个元素命名

    学生信息系统: (名字,年龄,性别,邮箱地址) 为了减少存储开支,每个学生的信息都以一个元组形式存放 如: ('tom', 18,'male','tom@qq.com' ) ('jom', 18,'m ...