每日一题 LeetCode 679. 24点游戏 【递归】【全排列】
题目链接
https://leetcode-cn.com/problems/24-game/
题目说明

题解
主要方法:递归 + 全排列
解释说明:
将 4 个数进行组合形成算式,发现除了 (a❈b)❈(c❈d) 的形式外,都可以通过单一的数字进行拆解,比如 (a*b+c)/d 可以逐步拆解成 (d -> (c -> (a -> (b))))。所以我们将特殊形式单独用全排列处理,一般情况用递归进行处理。
递归:
入口(数字list,目标值target)
下一步(取list中的一个值,将该值与target运算,算出list剩余的数应该得到怎样的值)
出口(list只剩一个值,与target相等)特殊处理: 对于 (a❈b)❈(c❈d) 的形式,将四个数字进行全排列后对每个可能的算式进行运算
代码示例:
class Solution:
def judgePoint24(self, nums: List[int]) -> bool:
#考虑精度误差,当结果与 24 的误差在 1e6 以内可以认为相等
EPSILON = 1e-6
def dfs(numbers, target):
if len(numbers) == 1:
return True if abs(numbers[0] - target) < EPSILON else False
for idx,num in enumerate(numbers):
tmp_nums = numbers[:]
tmp_nums.remove(num)
if dfs(tmp_nums, target - num) or dfs(tmp_nums, num - target) or (target and dfs(tmp_nums, num / target)) or (num and dfs(tmp_nums, target / num)):
return True
return False
if dfs(nums, 24):
return True
# 将 (a b) (c d) 的特殊情况单独处理,其他情况都可以从一个数逐步拆解
for num_arrange in list(permutations(nums)):
if (num_arrange[0] + num_arrange[1]) * (num_arrange[2] + num_arrange[3]) == 24 \
or (num_arrange[0] + num_arrange[1]) * (num_arrange[2] - num_arrange[3]) == 24 \
or (num_arrange[0] - num_arrange[1]) * (num_arrange[2] + num_arrange[3]) == 24 \
or (num_arrange[0] - num_arrange[1]) * (num_arrange[2] - num_arrange[3]) == 24 \
or ((num_arrange[2] + num_arrange[3]) and (num_arrange[0] + num_arrange[1]) / (num_arrange[2] + num_arrange[3]) == 24) \
or ((num_arrange[2] - num_arrange[3]) and (num_arrange[0] + num_arrange[1]) / (num_arrange[2] - num_arrange[3]) == 24) \
or ((num_arrange[2] + num_arrange[3]) and (num_arrange[0] - num_arrange[1]) / (num_arrange[2] + num_arrange[3]) == 24) \
or ((num_arrange[2] - num_arrange[3]) and (num_arrange[0] - num_arrange[1]) / (num_arrange[2] - num_arrange[3]) == 24):
return True
return False
大神的暴力美学
https://leetcode.com/problems/24-game/discuss/107675/Short-Python
def helper(nums):
print(nums)
if len(nums) == 1:
return math.isclose(nums[0], 24)
return any(helper((x,) + tuple(rest)) for a, b, *rest in permutations(nums) for x in
{a + b, a - b, a * b, b and a / b})
return helper(tuple(nums))
每日一题 LeetCode 679. 24点游戏 【递归】【全排列】的更多相关文章
- Java实现 LeetCode 679 24 点游戏(递归)
679. 24 点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: ...
- Leetcode 679.24点游戏
24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) ...
- Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)
Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...
- [leetcode] 679. 24 Game (Hard)
24点游戏,游戏规则就是利用().+.-. *. /,对四个数字任意运算,可以得出24点则为true. 排列组合问题,最多有A42*A32*A22*4*4*4,也就是12*6*2*4*4=9216种组 ...
- [LeetCode] 679. 24 Game(回溯法)
传送门 Description You have 4 cards each containing a number from 1 to 9. You need to judge whether the ...
- 每日一题-——LeetCode(121)买卖股票的最佳时机
题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格.如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润.注意你不能在买入股票前卖出股票 ...
- 每日一题-——LeetCode(78)子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3], [1], [2], [1,2,3], [1,3], [2, ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- 每日一题-——LeetCode(486) 预测赢家
题目描述: 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到 ...
随机推荐
- The Unique MST(最小生成树的唯一性判断)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- 【Maven】maven脚本中的maven.test.skip和skipTests的区别
命令 两种方式跳过编译 test mvn clean install -DskipTests mvn clean install -Dmaven.test.skip=true -DskipTests, ...
- DVWA_sql injection(low)
这里主要记录下做题时的思路以及步骤,不查看源码也不对源码进行分析.(我这个dvwa是一个在线靶场,所以我也不确定是否与本地靶场有些许出入) 1.Low 将难度调为Low级别后,来到如下界面 首先输入一 ...
- .net core Configuration对象
前因:最近在阅读.net core源码,发现关于Configuration介绍的文档都比较多,但是都比较杂乱,(微软文档太官方),所以写下一些自己的感想 主要通过三种使用情况来介绍 Web应用程序使用 ...
- jmeter连接redis取数据
1.导入fastjson-1.2.2.jar.jedis-2.2.1.jar到 jmeter\lib\ext\ 下 2.新建BeanShell Sampler import com.alibaba.f ...
- why哥被阿里一道基础面试题给干懵了,一气之下写出万字长文。
这是why的第 65 篇原创文章 荒腔走板 大家好,我是 why,欢迎来到我连续周更优质原创文章的第 65 篇.老规矩,先荒腔走板聊聊技术之外的东西. 上面这图是去年的成都马拉松赛道上,摄影师抓拍的我 ...
- get、post请求方式在jmeter中使用步骤
jmeter:性能测试工具,压测 一.jmeter工具测试接口时使用步骤: 1.测试计划右键--添加--Threads(Users)--线程组(线程数就是并发数) 2.线程组右键--Sampler-- ...
- java 多线程-3
十.同步机制解决Thread继承安全问题 创建三个窗口买票,共100张票.用继承来实现 方式一:同步代码块 public class RunMainExtends { public static vo ...
- Linux实战(5):Centos8安装python
Centos8正式版已经发布了,已经尝鲜的小伙伴们会发现与其他Linux发行版不同,CentOS 8默认不安装Python.接下来的操作指导大家如何安装python3. 转自链接 安装python3 ...
- 云计算openstack核心组件——glance— 镜像服务(6)
一.glance介绍: Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚 ...