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. 学会github管理项目与VS单元测试——第二次作业

    GIT地址 https://github.com/Bing-Fairy/AchaoCalculator GIT用户名 Bing—Fairy 学号后五位 62502 博客地址 https://home. ...

  2. 国际化(i18n) 各国语言缩写

    internationalization (国际化)简称:i18n,因为在i和n之间还有18个字符,localization(本地化 ),简称L10n. 一般用语言_地区的形式表示一种语言,如:zh_ ...

  3. 简单理解和使用 C# 委托与事件

    委托和事件 委托:委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程 ...

  4. webpack loader和插件的编写原理

    webpack自定义loader和插件的api网址:https://www.webpackjs.com/api/loaders/ 点击顶部API,看左侧api: 1. 如何编写一个loader 实现的 ...

  5. Centos7 minimal 安装npm

    最小版本缺少很多源,需要手动去添加源 如何去判断yum中 有没有 npm 的源呢 yum list | grep npm 如果是这样的,就代表需要自己去添加 curl -sL -o /etc/yum. ...

  6. sql server 中的表值函数和标量值函数

      顾名思义:表值函数返回的是表,而标量值函数可以返回基类型 一.表值函数 用户定义表值函数返回 table 数据类型.对于内联表值函数,没有函数主体:表是单个 SELECT 语句的结果集. 以下示例 ...

  7. vs 在高分屏下开发 winform 配置

    一.窗体控件大小 第一种方法:使用网格避免整除误差 在选项中将Windows窗体设计器的LayoutMode(布局模式)改成SnapToGrid(对齐到网格),并将Default Grid Cell ...

  8. RookeyFrame 线上 添加Model

    线上添加好了模块,会在本地生成几个文件 类文件:Rookey.Frame.Web\Config\TempModel\Order_File.code DLL文件:Rookey.Frame.Web\bin ...

  9. C++问题--error LNK2019: 无法解析的外部符号 __imp__wsprintfW

    一.问题 当编译运行C++连接Redis时,出现错误Win32_Interop_d.lib(Win32_ANSI.obj) : error LNK2019: 无法解析的外部符号 __imp__wspr ...

  10. $\text{fhq-treap}$总结

    \(\text{fhq-treap}\)总结 又名范浩强\(\text{treap}\),是一种无旋\(\text{treap}\).其原理同\(\text{treap}\)一样都是通过维护一个随机堆 ...