[LeetCode] 679. 24 Game(回溯法)
Description
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.
思路
题意:给定四张标有数字(1-9)的卡片,问通过加减乘除和小括号组成的表达式的结果能否等于24。
题解:考虑穷举法的话,四个数字共有4!种排列方法,4个数字中间加入符号共有4x4x4种方法,最后考虑小括号,小括号的放法共有(A(B(CD))、(A((BC)D)、((AB)(CD))、((A(BC))D)、(((AB)C)D)五种,那么种类最多有4!x4^3x5 = 7680种。
考虑回溯法,首先我们从集合A = {1 、2、3、4}中任意取出两个数,如取1、2,那么A = A - {1、2},对取出来的两个数字分别进行不同的四则运算,1 + 2、1 - 2……,将结果加入A中,有{3、3、4}、{-1,3,4}等,通过这种方法,将四个数降为三个数,然后降为两个数……
Java
class Solution {
boolean res = false;
final double esp = 1e-4;
public boolean judgePoint24(int[] nums) {
List<Double>list = new ArrayList<Double>();
for (int val:nums) list.add((double)val);
solver(list);
return res;
}
void solver(List<Double> array){
if (res) return;
if (array.size() == 1 && Math.abs(array.get(0) - 24.0) <= esp){
res = true;
return;
}
for (int i = 0;i < array.size();i++){
for (int j = 0;j < i;j++){
List<Double>list = new ArrayList<Double>();
Double p1 = array.get(i),p2 = array.get(j);
list.addAll(Arrays.asList(p1+p2,p1-p2,p2-p1,p1*p2));
//除数是否为0
if (Math.abs(p1) > esp){
list.add(p2/p1);
}
if (Math.abs(p2) > esp){
list.add(p1/p2);
}
array.remove(i);
array.remove(j);
for (Double val:list){
array.add(val);
solver(array);
array.remove(val);
}
array.add(j,p2);
array.add(i,p1);
}
}
}
}
[LeetCode] 679. 24 Game(回溯法)的更多相关文章
- Leetcode 679.24点游戏
24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) ...
- LeetCode刷题笔记-回溯法-括号生成
题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "( ...
- LeetCode刷题笔记-回溯法-分割回文串
题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...
- Java实现 LeetCode 679 24 点游戏(递归)
679. 24 点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: ...
- [leetcode]79.Search Word 回溯法
/** * Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- 每日一题 LeetCode 679. 24点游戏 【递归】【全排列】
题目链接 https://leetcode-cn.com/problems/24-game/ 题目说明 题解 主要方法:递归 + 全排列 解释说明: 将 4 个数进行组合形成算式,发现除了 (a❈b) ...
- [leetcode] 679. 24 Game (Hard)
24点游戏,游戏规则就是利用().+.-. *. /,对四个数字任意运算,可以得出24点则为true. 排列组合问题,最多有A42*A32*A22*4*4*4,也就是12*6*2*4*4=9216种组 ...
- LeetCode刷题笔记-回溯法-组合总和问题
题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...
- Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)
Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...
随机推荐
- hset和hget
- 面试(2)-java-se-HashSet和TreeSet12
Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...
- Spark算子--countByKey
转载请标明出处http://www.cnblogs.com/haozhengfei/p/1633ffc63e2c925e930adadc9528c830.html countByKey--Actio ...
- 使用python爬取百度贴吧内的图片
1. 首先通过urllib获取网页的源码 # 定义一个getHtml()函数 def getHtml(url): try: page = urllib.urlopen(url) # urllib.ur ...
- nginx服务器配置/websocket nginx 配置笔记
server { listen 80; server_name xxx.com; # add_header '*' ; location /u/ { # 反向代理透传客户端ip proxy_set_h ...
- Linux 中出现的-bash: syntax error near unexpected token `
版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux 5中导入数据时,出现下面的错误. -bash: syntax error near unexpected token `(' 检查了 ...
- Python3 引入模块的方法
例子 import random 产生随机整数 import random secret = random.randint(0,10)
- 利用overflow实现导航栏中常 出现的倒三角下拉小图标
常用网页界面中,导航栏中的倒三角下拉小图标实现,可用overflow: 效果如右图: .Triangle{position:relative;width:280px;height:15px;} ;ov ...
- Node.js之单利模式
在iOS中我们经常用到单利模式,这样就能够实现在多处共享同一数据,那么在Node.js中也存在这种模式. 我们知道,Node.js中模块的概念很重要,在写模块的接口的时候,只需要暴露出一个实例对象就能 ...
- [Qt Quick] qmlscene工具的使用
qmlscene是Qt 5提供的一个查看qml文件效果的工具.特点是不需要编译应用程序. qmlscene = qml + scene (场景) qmlscene.exe位于Qt的安装目录下 (类似/ ...