LeetCode679. 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 - 1is 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所有可能。对于给定的四个数,首先从中选两个数,然后对这两个数选择加减乘除这四种操作的一个,得出结果后从剩下的3个数中再选两个,执行一种操作。如此这样重复遍历所有可能即可。
class Solution {
boolean res = false;
final double eps = 0.001;
public boolean judgePoint24(int[] nums) {
List<Double> arr = new ArrayList<>();
for(int n: nums) arr.add((double) n);
helper(arr);
return res;
}
private void helper(List<Double> arr){
if(res) return;
if(arr.size() == 1){
if(Math.abs(arr.get(0) - 24.0) < eps) // java中double类型判断是否相等的方法,不能直接用是否等于0判断
res = true;
return;
}
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < i; j++) {
List<Double> next = new ArrayList<>();
Double p1 = arr.get(i), p2 = arr.get(j);
next.addAll(Arrays.asList(p1+p2, p1-p2, p2-p1, p1*p2));
if(Math.abs(p2) > eps) next.add(p1/p2);
if(Math.abs(p1) > eps) next.add(p2/p1);
arr.remove(i);
arr.remove(j);
for (Double n: next){
arr.add(n);
helper(arr);
arr.remove(arr.size()-1);
}
arr.add(j, p2);
arr.add(i, p1);
}
}
}
}
注意上面List集合的两个方法:
void add(int index,
E element)
E remove(int index)
- Parameters:
index- the index of the element to be removed- Returns:
- the element previously at the specified position
LeetCode679. 24 Game的更多相关文章
- [Swift]LeetCode679. 24点游戏 | 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
- CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能
CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...
- 【趣味分享】C#实现回味童年的24点算法游戏
一.24点游戏玩法规则效果展示 1.初始化界面 2.开始游戏界面 3.游戏超时界面 4.查看答案界面 5.答对界面 6.答错界面 7.计算表达式的验证界面 8.一副牌算完开始新一副牌界面 到这里24点 ...
- C#开发微信门户及应用(24)-微信小店货架信息管理
在前面微信小店系列篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及<C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试& ...
- [MySQL Reference Manual] 24 MySQL sys框架
24 MySQL sys框架 24 MySQL sys框架 24.1 sys框架的前提条件 24.2 使用sys框架 24.3 sys框架进度报告 24.4 sys框架的对象 24.4.1所有sys下 ...
- mysql 5.6.24安装实例
安装前准备工作: 1)编辑PATH路径 vim /etc/profile PATH=/home/mysql/bin:/home/mysql/lib:$PATH export PATH 2)生效PATH ...
- C语言-纸牌计算24点小游戏
C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
- .NET开发人员必看:提高ASP.NET Web应用性能的24种方法和技巧
那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...
随机推荐
- Wifi密码破解实战
原文链接地址:http://www.freebuf.com/articles/wireless/127261.html https://www.baidu.com/?tn=98012088_4_dg& ...
- 【bzoj3173】最长上升子序列
Portal --> bzoj3173 Solution 感觉自己需要智力康复qwq 首先题目给的这个序列肯定是一个\(1-n\)的排列,并且插入的顺序是从小到大 仔细思考一下会发现如果知道了最 ...
- 根据数据库连接的java.sql.Connection获取数据库名称
// 获取数据库的元数据信息 DatabaseMetaData metaData = conn.getMetaData(); // 获取数据库名称的方法 System.out.println(meta ...
- activiti学习-用户与用户组
activiti学习笔记3-用户与用户组 2015年05月07日 14:43:06 cq1982 阅读数:4142更多 个人分类: activiti工作流引擎 (本博客都是纯文本手工代码,错误难免 ...
- Cyrus SASL介绍(翻译)
http://blog.sina.com.cn/s/blog_7695e9f40100pnpa.html Cyrus SASL介绍 1. 综述 这篇文档讲述的是系统管理员配置SASL的方法,其中详细的 ...
- STL源码分析-algorithm
http://note.youdao.com/noteshare?id=8b3473983e4c8d8eee32544708633f79
- 「Python」python与微信
pip3 install itchat 主要用到的方法: itchat.login() 微信扫描二维码登录 itchat.get_friends() 返回完整的好友列表,每个好友为一个字典, 其中第一 ...
- 从一个集合中过滤另一个集合中存在的项(类似in)
直接贴代码出来: List<PriceMark> list = PriceMarkDAL.m_PriceMarkDAL.GetList("Erp_ProName='TLC7528 ...
- python---爬虫相关性能(各个异步模块的使用,和自定义异步IO模块)
一:线程池,进程池等相关文章了解 python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用) python---基础知识回顾(十)进程和线程(协程gevent:线程在 ...
- input模拟
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...