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-& ...
 
随机推荐
- selenium 全天课整理(二)
			
# encoding=utf-8 ''' selenium 全天 二 ''' #unittest例子 from selenium import webdriver import unittest,ti ...
 - 一种使用gitlab的CI/CD功能实现Nginx配置更新的方法
			
至于nginx的docker制作,前面已介绍过. 现在使用gitlab在线编辑的方式,可实现Nginx的自定义配置并更新. .gitlab-ci.yml内容如下: variables: project ...
 - linux系统编程之文件与io(四)
			
今天继续学习文件与io,主要是学习文件共享及文件.复制文件描述符,有点抽象,主要是概念上的理解,但是很重要,下面一一来分解: 文件共享: 回顾一下,在linux系统调用中,是通过文件描述符来访问文件的 ...
 - Mysql存储引擎中InnoDB与Myisam的区别
			
1. 事务处理innodb 支持事务功能,myisam 不支持.Myisam 的执行速度更快,性能更好. 2. select ,update ,insert ,delete 操作MyISAM:如果执行 ...
 - maven的使用和环境搭建
			
请在博客分类的未分类中找到这篇文章
 - httpclient工具使用(org.apache.httpcomponents.httpclient)
			
httpclient工具使用(org.apache.httpcomponents.httpclient) 引入依赖 <dependency> <groupId>org.apac ...
 - z-tree的使用bug
			
最近折腾了下z-tree,这个存在一个bug: 新增icon出不来,废话少说上代码: <style type="text/css"> .ztree li span.bu ...
 - iota妙用
			
itoa可以套公式,下面的依旧会按照公式运算 package main import "fmt" func main() { const ( b = 1 << (10 ...
 - C# 调用 C++ Dll 类型转换的方式 全
			
摘要:C#引用C++ Dll 所有类型转换的方式 //C++中的DLL函数原型为 //extern "C" __declspec(dllexport ...
 - nodejs新工具-cypress和testcofe的崛起
			
今天咨询一个自动化 工具问题,偶然间有人提起了这个可能以后会很火的工具,在此找到一篇很好的参考文章 记录并为以后做准备 cypress和testcofe https://www.jianshu.com ...