题目链接

https://leetcode-cn.com/problems/24-game/

题目说明

题解

主要方法:递归 + 全排列

解释说明:

  1. 将 4 个数进行组合形成算式,发现除了 (a❈b)❈(c❈d) 的形式外,都可以通过单一的数字进行拆解,比如 (a*b+c)/d 可以逐步拆解成 (d -> (c -> (a -> (b))))。所以我们将特殊形式单独用全排列处理,一般情况用递归进行处理。

  2. 递归:

    入口(数字list,目标值target)

    下一步(取list中的一个值,将该值与target运算,算出list剩余的数应该得到怎样的值)

    出口(list只剩一个值,与target相等)

  3. 特殊处理: 对于 (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点游戏 【递归】【全排列】的更多相关文章

  1. Java实现 LeetCode 679 24 点游戏(递归)

    679. 24 点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: ...

  2. Leetcode 679.24点游戏

    24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) ...

  3. Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)

    Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...

  4. [leetcode] 679. 24 Game (Hard)

    24点游戏,游戏规则就是利用().+.-. *. /,对四个数字任意运算,可以得出24点则为true. 排列组合问题,最多有A42*A32*A22*4*4*4,也就是12*6*2*4*4=9216种组 ...

  5. [LeetCode] 679. 24 Game(回溯法)

    传送门 Description You have 4 cards each containing a number from 1 to 9. You need to judge whether the ...

  6. 每日一题-——LeetCode(121)买卖股票的最佳时机

    题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格.如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润.注意你不能在买入股票前卖出股票 ...

  7. 每日一题-——LeetCode(78)子集

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  [1,2,3],  [1,3],  [2, ...

  8. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  9. 每日一题-——LeetCode(486) 预测赢家

    题目描述: 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到 ...

随机推荐

  1. 推荐一个IT老鸟肝了2月有余的免费开源WPF企业级开发框架

    一个新学WPF的IT老鸟,肝了2个月做了这么一个WPF企业级开发框架,站长clone学习,觉得甚是不错.这是一个使用了Prism搭建的插件式框架,封装了DataGrid的使用,使整个框架子模块简单易学 ...

  2. 0vscode基本插件

    Bracket Pair Colorizer auto-close-tag Auto Rename Tag Bracket Pair Colorizer Dracula ESLint  Code Sp ...

  3. leetcode刷题-77组合

    题目 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3 ...

  4. 一条 SQL 引发的事故,同事直接被开除!!

    前言 Insert into select请慎用. 这天xxx接到一个需求,需要将表A的数据迁移到表B中去做一个备份.本想通过程序先查询查出来然后批量插入.但xxx觉得这样有点慢,需要耗费大量的网络I ...

  5. [LeetCode]678. 有效的括号字符串、20. 有效的括号(栈)

    题目 678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何 ...

  6. [LeetCode] 17. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

  7. Spring Cloud Alibaba微服务生态的基础实践

    目录 一.背景 二.初识Spring Cloud Alibaba 三.Nacos的基础实践 3.1 安装Nacos并启动服务 3.2 建立微服务并向Nacos注册服务 3.3 建立微服务消费者进行服务 ...

  8. 需要完成PAT作业和微博作业的具体方法

    http://www.cnblogs.com/c-programing-language/p/6703508.html

  9. Ansys Student 2020R2中Fluent编译UDF简介

    使用内建编译器 在Ansys Fluent中编译UDF一般都需要额外安装相应版本的Visual Studio编译器,VS的缺点是体量大,占空间,安装后还需要额外进行相关设置才能正常使用.而新版本的An ...

  10. 基于Excel参数化你的Selenium2测试-xlrd

    本篇文章转载至苦叶子:   前言 今天我们就如何使用xlrd模块来进行python selenium2 + excel自动化测试过程中的参数化进行演示说明,以解决大家在自动化测试实践过程中参数化的疑问 ...