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-& ...
随机推荐
- 介绍一个二次排序的小技巧(best coder27期1001jump jump jump)
先来描述一下问题: 问题描述 有n小孩在比赛跳远,看谁跳的最远.每个小孩可以跳3次,这个小孩的成绩就是三次距离里面的最大值.例如,一个小孩跳3次的距离分别时10, 30和20,那么这个小孩的成绩就是3 ...
- Vue项目中自动将px转换为rem
一.配置与安装步骤: 1.在 Vue 项目的 src 文件夹下创建一个 config 文件夹: 2.在 config 文件夹中创建 rem.js: 3.将以下代码复制到 rem.js 中: // 基准 ...
- python中while循环的基本使用
一.while循环 while 条件: 如果条件为True,会一直循环 代码块(循环体) else: 当上面的条件为假.才会执行 执行顺序:判断条件是否为真.如果真,执行循环 ...
- python打造漏洞数据导出工具
功能 [x] 支持导出的数据:IP地址.漏洞名称.风险等级.整改建议.漏洞描述.漏洞CVE编号.漏洞对应端口.漏洞对应协议.漏洞对应服务等. [x] 导出不同端口的同一个漏洞,也就是一个端口对应一个漏 ...
- 开启idea自动Build功能
修改Intellij IDEA的配置两步:1.setting -> Compile -> Build project automatically --> 选中 2.CTRL + SH ...
- Python高级编程和异步IO并发编程(笔记)
一.魔法函数 # 例子 class Company(object): def __init__(self, employee_list): self.employee = employee_list ...
- Flask - 四剑客 | templates | 配置文件 | 路由系统 | CBV
Flask框架简介 说明:flask是一个轻量级的web框架,被称为微型框架.只提供了一个高效稳定的核心,其它全部通过扩展来实现.意思就是你可以根据项目需要进行量身定制,也意味着你需要不断学习相关的扩 ...
- Oracle中日期作为条件的查询
1.范围日期的查询: select * from goods where g_time betweento_date('2018/12/26 10:01:59','yyyy-MM-dd hh:mi:s ...
- 在idea中调试spark程序-配置windows上的 spark local模式
spark程序大致有如下运行模式: standalone模式:spark自带的模式 spark on yarn:利用hadoop yarn来做集群的资源管理 local模式:主要在测试的时候使用, 这 ...
- linux共享文件 - samba 服务器
1.Samba 服务器 客户端 yum 安装: # yum install samba samba-client -y 2.samba 配置文件配置 /etc/samba/smb.conf [glo ...