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. WebForm SignalR 实时消息推送

    原文:https://www.jianshu.com/p/ae25d0d77011 官方文档:https://docs.microsoft.com/zh-cn/aspnet/signalr/ 实现效果 ...

  2. mysql启动错误:Starting MySQL.. ERROR! The server quit without updating PID file错误

    1.原因是因为 mysql-bin.0000*的文件占满系统盘,磁盘空间不足导致无法写入. 解决方法: 如果不需要二进制日志文件,二进制文件可以实现数据库复制以及即时点恢复. 查看日志在配置文件的da ...

  3. 微信小程序~App.js中获取用户信息

    (1)代码:主要介绍下获取用户信息部分 onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] l ...

  4. Java - MyBites 逆向工程

    逆向工程是什么呢? 说白了就是 mybatis 中提供了一个可以让你从 已经创建好的 数据库中,去通过表名,生成对应类,类属性和XML文件(sql语句). 源码:mybatis_AutoGenerat ...

  5. jquery操作按钮修改对应input属性

    点击右侧的修改按钮,对应的input中的disabled和readonly得属性修改 $(".buttonxg button").click(function(){ $(this) ...

  6. oracle: jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111

    https://www.cnblogs.com/mmlw/p/5808072.html org.mybatis.spring.MyBatisSystemException: nested except ...

  7. 使用Default Trace查看谁还原了你的数据库?

    select e.nameaseventclass,t.loginname,t.spid,t.starttime, t.textdata,t.objectid,t.objectname,t.datab ...

  8. 自用ajxa 后台管理请求

    /** * 保存或者修改商品信息 * @returns */ function saveOrUpdateBaseGoodInfo(){ var json={}; var goodName=$.trim ...

  9. 配置django的环境实现外部脚本调用django中的模型类

    通过导入os模块,os设置django的settings文件,配置好django项目的环境,然后执行django.set_up()使环境生效,然后就可以导入模型类,使用增删改查

  10. 1-STM32+W5500+GPRS物联网开发基础篇-工控板简介

    最近这些日子都在忙活STM+W5500+GPRS的板子,所以前面的那块板子的教程耽搁了些时间. 这次的板子和上一版相比更贴近了使用,是因为有朋友督促我要做一块直接可以在工厂使用的板子,所以设计了这一块 ...