Leetcode: 24 Game
时间:2019-10-10
本文章向大家介绍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 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.
1 class Solution { 2 public boolean judgePoint24(int[] nums) { 3 List<Double> list = new ArrayList<>(); 4 for (int num : nums) { 5 list.add((double)num); 6 } 7 return backtracking(list); 8 } 9 10 public boolean backtracking(List<Double> list) { 11 if (list.size() == 1) { 12 if (Math.abs(list.get(0) - 24.0) < 0.001) { 13 return true; 14 } 15 return false; 16 } 17 18 // every time backtracking: always draw two cards 19 for (int i = 0; i < list.size(); i ++) { 20 for (int j = i + 1; j < list.size(); j ++) { 21 22 // for each possible result of the two card-combination 23 for (double c : compute(list.get(i), list.get(j))) { 24 List<Double> nextRound = new ArrayList<>(); 25 nextRound.add(c); 26 27 for (int k = 0; k < list.size(); k ++) { 28 if (k != i && k != j) 29 nextRound.add(list.get(k)); 30 } 31 32 if (backtracking(nextRound)) return true; 33 } 34 } 35 } 36 return false; 37 } 38 39 // compute the possible result of a combination 40 public List<Double> compute(double a, double b) { 41 return Arrays.asList(a + b, a - b, b - a, a * b, a / b, b / a); 42 } 43 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/11647624.html
- Linux 基础:文件查找 find
- PHP 闭包(Closure)初探
- linux服务器apache服务配置wordpress伪静态
- 用 Python 实现一个大数据搜索引擎
- 关于 Java 你不知道的 10 件事
- 如何在一周之内获得GitHub stars 3500+ —为什么对于程序员这是如此的重要
- 程序员做完整性检查的命令行工具
- Spring MVC工作原理
- PHP 中被忽略的性能优化利器:生成器
- 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解
- 从放弃迅雷到自己开发下载工具
- SSH 登录流程分析
- 从 0 到 1 优雅的实现PHP多进程管理
- 爬虫抓取的门道——来看这篇
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- java多线程关键字volatile、lock、synchronized
- 操作系统基础 - 线程级并发
- SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
- 《内网安全攻防》学习笔记,第二章-域内信息收集
- 献给每一位母亲
- JUC学习之生产者和消费者
- 在 WSL 2 中基于 Docker 编排 LNMP 运行环境
- PHP 魔术方法、序列化与对象复制
- PHP 静态属性和静态方法
- 通过 Trait 水平扩展 PHP 类功能
- 通过对象组合水平扩展 PHP 类功能
- DO,DTO,VO,POJO 你知道吗?
- Python 爬虫进阶必备 | AES-CBC 的 Pyhon 实现要怎么写?给代码就完事了
- Babel:下一代Javascript语法编译器
- 如何定位及优化SQL语句的性能问题