Leetcode: 24 Game
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的更多相关文章
- [LeetCode] 24 Game 二十四点游戏
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- [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 ...
- Java实现 LeetCode 24 两两交换链表中的节点
24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...
- leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表
24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...
- [leetcode 24] Swap Nodes in k-Group
1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...
- leetcode 24
链表操作的,要注意标记头结点和边界问题. 代码如下: ListNode *swapPairs(ListNode *head) { if(head==NULL||head->next==NULL) ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
随机推荐
- yum 异常解决一例
http://mirrors.cloud.aliyuncs.com/centos/6/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22 ...
- python链式对比
参考 https://www.cnblogs.com/shanghongyun/p/10519579.html 为什么Python中“2==2>1”结果为True在Python中,你可能会发现这 ...
- IDEA实用教程(九)—— 创建Servlet
4. 创建Servlet 1) 第一步 2) 第二步 3) 第三步 4) 第四步 由于新创建的Web项目, 没有Tomcat环境, 所以创建的Servlet会发生导包错误,如下图所示 : 因此我们需要 ...
- Linux postfix配置方法
第七题 配置邮件服务器 postfix学习网站:https://blog.csdn.net/mycms5/article/details/78773308 system1和systemc2分别执行 ...
- 《hello-world》第九次团队作业:【Beta】Scrum meeting 2
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 <hello--wor ...
- Alpha冲刺随笔六:第六天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...
- 2019-2020-1 20199312《Linux内核原理与分析》第七周作业
进程的描述 操作系统内核实现操作系统的三大管理功能 进程管理(内核中最核心的功能) 内存管理 文件系统 在操作系统中,我们通过进程控制块PCB描述进程. 为了管理进程,内核必须对每个进程进行清晰的描述 ...
- python - djanog (静态文件)
# 在 setting 文件中的 static ,通过这个方法(别名) 可以拼接到其它文件夹中的文件 # 第一步: 导入 # {% load static %} # 第二步: 查找 static (别 ...
- Python + Apache Kylin 让数据分析更加简单!
现如今,大数据.数据科学和机器学习不仅是技术圈的热门话题,也是当今社会的重要组成.数据就在每个人身边,同时每天正以惊人的速度快速增长,据福布斯报道:到 2025 年,每年将产生大约 175 个 Zet ...
- YAML_04 用user模块添加用户,并修改密码
ansible]# vim user.yml --- - hosts: cache remote_user: root vars: username: lisi tasks: ...