leetcode 679. 24 Game

本文探讨了如何通过加减乘除操作使四个1至9之间的数字等于24的问题。采用深度优先搜索策略,利用分数结构处理除法运算,确保所有可能的操作组合都被考虑。示例包括成功的解决方案和不成功的案例。

67924 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:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. 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.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

题目:每个数字都必须用到,且只能用一次。

1、DFS。

2、取出数组中两个数,然后相互加减乘除。

3、为了处理除法,所以用一个shu结构来存储分母分子。


class shu{
public:
    int fenzi;
    int fenmu;
    shu(int x, int y = 1):fenzi(x), fenmu(y){}
};

class Solution {
public:
    bool judgePoint24(vector<int>& nums)
    {
        vector<shu> num;
        for (auto it : nums)
            num.push_back(shu(it));
        return helper(num);
    }
    
    bool helper(vector<shu>& nums)  //成功返回1
    {
        if (nums.size() == 1 && nums[0].fenmu != 0 && nums[0].fenzi % nums[0].fenmu == 0 && nums[0].fenzi / nums[0].fenmu == 24)
            return true;
        
        int size = nums.size();
        for (int i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++)
            {
                vector<shu> tmp_p = nums;
                tmp_p.erase(tmp_p.begin() + i);
                tmp_p.erase(tmp_p.begin() + j - 1);
                tmp_p.push_back( shu(nums[i].fenzi*nums[j].fenmu + nums[i].fenmu * nums[j].fenzi, nums[i].fenmu * nums[j].fenmu));
                if (helper(tmp_p)) return true;
                
                vector<shu> tmp_j = nums;
                tmp_j.erase(tmp_j.begin() + i);
                tmp_j.erase(tmp_j.begin() + j - 1);
                tmp_j.push_back( shu(abs(nums[i].fenzi * nums[j].fenmu - nums[i].fenmu * nums[j].fenzi), nums[i].fenmu * nums[j].fenmu));
                if (helper(tmp_j)) return true;
                
                vector<shu> tmp_c = nums;
                tmp_c.erase(tmp_c.begin() + i);
                tmp_c.erase(tmp_c.begin() + j - 1);
                tmp_c.push_back( shu(nums[i].fenzi * nums[j].fenzi, nums[i].fenmu * nums[j].fenmu));
                if (helper(tmp_c)) return true;

                vector<shu> tmp_d1 = nums;
                tmp_d1.erase(tmp_d1.begin() + i);
                tmp_d1.erase(tmp_d1.begin() + j - 1);
                tmp_d1.push_back( shu(nums[i].fenzi * nums[j].fenmu, nums[i].fenmu * nums[j].fenzi));
                if (helper(tmp_d1)) return true;
                
                vector<shu> tmp_d2 = nums;
                tmp_d2.erase(tmp_d2.begin() + i);
                tmp_d2.erase(tmp_d2.begin() + j - 1);
                tmp_d2.push_back( shu(nums[i].fenmu * nums[j].fenzi, nums[i].fenzi * nums[j].fenmu));
                if (helper(tmp_d2))  return true;
            }
        }
        return false;
    }
};




<think>好的,我现在需要帮助用户查找LeetCode热题100的相关练习和解析。首先,我应该回顾用户提供的引用内容,看看有没有相关的信息可以整合。 引用[1]提到了回溯算法的一些题目,比如组合问题、分割问题、子集问题等,这些可能属于热题100的一部分。引用[2]中的跳跃游戏和合并区间,引用[3]的汇总区间,这些都是常见的算法题,可能在热题100中出现。 用户需要的是热题100的汇总和解析,所以我需要按照不同的算法分类整理题目。比如分回溯、贪心、动态规划等。根据引用中的内容,可以列出每个类别下的题目,比如回溯部分包括组合问题、分割问题等,贪心有跳跃游戏,动态规划有最长回文子串。 同时,用户提到要生成相关问题,我需要考虑用户可能的后续问题,比如如何准备、解题技巧或高频题解析。需要确保回答结构清晰,分列出,并给出每个题目的链接或题号,方便用户查找。 还要注意用户之前的引用内容,可能涉及到的题目如组合总和、复原IP地址、子集等,这些都需要包含在相应的分类中。另外,动态规划部分引用[2]提到了最长回文子串,这也是热题100中的经典题目。 最后,要确保回答符合格式要求,使用中文,正确应用LaTeX格式,行内公式用$...$,独立公式用$$...$$。同时,在末尾添加相关问题,帮助用户深入探讨。检查是否有遗漏的题目或分类,确保信息全面准确。</think>以下是根据LeetCode热题100整理的分类汇总及解析,结合高频题型与解题技巧,帮助您系统复习: --- ### 一、回溯算法 1. **组合问题** - [39.组合总和](https://leetcode.cn/problems/combination-sum/) 关键:允许重复选元素,剪枝优化(排序后跳过后续无效分支) - [40.组合总和II](https://leetcode.cn/problems/combination-sum-ii/) 关键:去重逻辑(同一层跳过相同元素)[^1] 2. **分割问题** - [131.分割回文串](https://leetcode.cn/problems/palindrome-partitioning/) 关键:预处理回文子串(动态规划或中心扩展) - [93.复原IP地址](https://leetcode.cn/problems/restore-ip-addresses/) 关键:分段验证有效性(数值范围、前导零处理) 3. **排列问题** - [46.全排列](https://leetcode.cn/problems/permutations/) 关键:标记已选元素,回溯时撤销状态 --- ### 二、贪心算法 1. [55.跳跃游戏](https://leetcode.cn/problems/jump-game/) **核心思路**:维护最大可达距离,若当前索引超过最大距离则失败[^2] ```python def canJump(nums): max_reach = 0 for i in range(len(nums)): if i > max_reach: return False max_reach = max(max_reach, i + nums[i]) return True ``` 2. [122.买卖股票的最佳时机II](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/) **贪心策略**:所有上升区间的利润累加 --- ### 三、动态规划 1. [5.最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/) **状态定义**:$dp[i][j]$表示子串$s[i..j]$是否为回文 **转移方程**: $$dp[i][j] = (s[i] == s[j]) \ \text{且} \ dp[i+1][j-1] = \text{True}$$ 2. [70.爬楼梯](https://leetcode.cn/problems/climbing-stairs/) **状态转移**:$dp[n] = dp[n-1] + dp[n-2]$(斐波那契数列) --- ### 四、双指针 1. [11.盛最多水的容器](https://leetcode.cn/problems/container-with-most-water/) **关键**:左右指针向中间收敛,每次移动高度较小的指针 2. [15.三数之和](https://leetcode.cn/problems/3sum/) **关键**:排序后固定一个数,转化为两数之和问题(需去重) --- ### 五、数据结构 1. [146.LRU缓存](https://leetcode.cn/problems/lru-cache/) **实现要**:哈希表+双向链表(快速定位节与调整顺序) 2. [23.合并K个升序链表](https://leetcode.cn/problems/merge-k-sorted-lists/) **优化方法**:优先队列(小根堆)合并 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值