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:
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
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.
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

Backtracking: every time draw two cards, calculate the possible results, and add one result to the nextRound list.

The nextRound list will have the remaining unused cards.

Every round the nextRound list will decrease its size by 1.

Repeat the process until nextRound list decrease to size 1.

 class Solution {
public boolean judgePoint24(int[] nums) {
List<Double> list = new ArrayList<>();
for (int num : nums) {
list.add((double)num);
}
return backtracking(list);
} public boolean backtracking(List<Double> list) {
if (list.size() == 1) {
if (Math.abs(list.get(0) - 24.0) < 0.001) {
return true;
}
return false;
} // every time backtracking: always draw two cards
for (int i = 0; i < list.size(); i ++) {
for (int j = i + 1; j < list.size(); j ++) { // for each possible result of the two card-combination
for (double c : compute(list.get(i), list.get(j))) {
List<Double> nextRound = new ArrayList<>();
nextRound.add(c); for (int k = 0; k < list.size(); k ++) {
if (k != i && k != j)
nextRound.add(list.get(k));
} if (backtracking(nextRound)) return true;
}
}
}
return false;
} // compute the possible result of a combination
public List<Double> compute(double a, double b) {
return Arrays.asList(a + b, a - b, b - a, a * b, a / b, b / a);
}
}

Leetcode: 24 Game的更多相关文章

  1. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  2. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. Java实现 LeetCode 24 两两交换链表中的节点

    24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...

  4. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  5. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  6. leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表

    24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...

  7. [leetcode 24] Swap Nodes in k-Group

    1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...

  8. leetcode 24

    链表操作的,要注意标记头结点和边界问题. 代码如下: ListNode *swapPairs(ListNode *head) { if(head==NULL||head->next==NULL) ...

  9. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

随机推荐

  1. 【独家】K8S漏洞报告 | CVE-2019-1002101解读

    kubectl cp漏洞CVE-2019-1002101分析 Kube-proxy IPVS添加flag ipvs-strict-arp 近期bug fix数据分析 ——本期更新内容 kubectl ...

  2. 十大排序代码实现(python)

    目录 冒泡排序 快速排序 简单插入排序 希尔排序 简单选择排序 堆排序 二路归并排序 多路归并排序 计数排序 桶排序 基数排序 写在前面: 参考文章:十大经典排序算法 本文的逻辑顺序基于从第一篇参考博 ...

  3. Kotlin编译器优化与when关键字详解

    Any类型: 定义一个函数,其参数接受所有类型,对于Java而言Object是所有类的基类,而在Kotlin中得用Any关键字,如下: 其中瞅一下该Any字段是个啥类型: 然后里面做一些判断: 这是因 ...

  4. NET Framework 的泛型

    NET Framework 的泛型 泛型是具有占位符(类型参数)的类.结构.接口和方法,这些占位符是类.结构.接口和方法所存储或使用的一个或多个类型的占位符.泛型集合类可以将类型参数用作它所存储的对象 ...

  5. sublime——开启自动保存

    前言 懒 步骤 失去焦点自动保存 "save_on_focus_lost": true 首选项-->设置-->Ctrl+F搜索‘save’,找到“save_on_foc ...

  6. VS Code中配置Markdown

    其实,对我来说是反过来的,我是为了使用Markdown而安装VS Code(虽然久仰大名) 安装VS Code 安装Markdown插件 使用篇 1. 安装vscode 之所以啰嗦一下,是因为据说安装 ...

  7. Fiddler拦截请求

    bpu (breakpoint url ) Create a request breakpoint for URIs containing the specified string. Setting ...

  8. hello world 程序的生成过程

    一个c / c ++文件需要经过预先(预处理),编译(编译),编译(汇编)和链接(链接)等四步,才能生成可执行程序. 在日常编译中,通常“编译”统称这四步: gcc -c xxx .s:汇编 gcc ...

  9. 【CSS】div

    一.div內容溢出 .remark-div { overflow: auto; height: auto; max-height: 100px; } 1.溢出 overflow :auto时,内容超过 ...

  10. UVA 1613 K度图染色

    题目 \(dfs+\)证明. 对于题目描述,可以发现\(K\)其实就是大于等于原图中最大度数的最小奇数,因为如果原图度数最大为奇数,则最多颜色肯定为K,而如果原图最大度数为偶数,则\(K\)又是奇数, ...