【DFS】子集(两种递归策略)

一、题目

. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/TVdhkn/submissions/544682490/

二、题解

枚举当前元素选或者不选

对应这样一个序列1 2 3, 对于这个序列的第一个元素,在最终的答案中对于每一个位置的元素,都可以选择选他或者不选他

class Solution {
public:
    vector<vector<int>> res;
    vector<int> nums;
    vector<int> path;
    int n;

    void dfs(int u) {
        if (u == n) {
            res.push_back(vector<int>(path));
            return;
        }
        // 选择当前元素
        path.push_back(nums[u]);
        // 选择当前元素的分支
        dfs(u + 1);
        // 回复现场
        path.pop_back();
        // 不选当前元素的分支
        dfs(u + 1);
    }

    vector<vector<int>> subsets(vector<int>& _nums) {
        nums = _nums;
        n = nums.size();
        dfs(0);
        return res;
    }
};
枚举每个位置的情况

枚举每一个位置可能的情况,比如1 2 3这个序列,我们每次都去枚举当前位置可能的元素,比如刚开始是空,将空加入结果集合,然后第一个位置可以枚举1,当第一个位置枚举1之后,第二个位置如何选择呢,第二个位置一定是从1这个元素之后去选择,可以选择2、3变为1 2 或者 13,继续当第二位置选择2时,我们需要从2后面继续选择第三个位置作为1 2 这个序列的第三个元素,2之后只能选择3,如果第二个位置选时,我们需要从3后面选择13序列的第三个元素,3后面没有就不能选择

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path, nums;
    int n;

    void dfs(int u) {
        // 每一个节点都加入
        res.push_back(path);

        // 遍历当前每一个可以选择的元素
        for (int i = u; i < n; i++) {
            path.push_back(nums[i]);
            dfs(i + 1);
            path.pop_back();
        }
    }

    vector<vector<int>> subsets(vector<int>& _nums) {
        nums = _nums;
        n = nums.size();
        dfs(0);
        return res;
    }
};
class Solution {
    List<List<Integer>> res;
    int[] nums;
    List<Integer> path;
    boolean check[];
    int n;

    public void dfs(int u) {
        res.add(new ArrayList<>(path));
        for (int i = u; i < n; i++) {
            path.add(nums[i]);
            dfs(i + 1);
            path.remove(path.size() - 1);
        }
    }

    public List<List<Integer>> subsets(int[] _nums) {
        res = new ArrayList<>();
        nums = _nums;
        n = nums.length;
        check = new boolean[n];
        path = new ArrayList<>();
        dfs(0);
        return res;
    }
}

相关推荐

  1. 【leetcode】DFS&题目总结

    2024-07-11 12:44:01       30 阅读
  2. 使用,手写实现数组的 flat 方法,方法

    2024-07-11 12:44:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 12:44:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 12:44:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 12:44:01       57 阅读
  4. Python语言-面向对象

    2024-07-11 12:44:01       68 阅读

热门阅读

  1. RAG技术知识笔记

    2024-07-11 12:44:01       27 阅读
  2. C# 泛型

    2024-07-11 12:44:01       25 阅读
  3. Spring AOP 基础知识

    2024-07-11 12:44:01       23 阅读
  4. PHP MySQL 简介

    2024-07-11 12:44:01       23 阅读
  5. linux 文件末尾追加内容

    2024-07-11 12:44:01       22 阅读
  6. 从IE到Edge:微软浏览器的演变与未来展望

    2024-07-11 12:44:01       23 阅读
  7. 浅谈ES6

    2024-07-11 12:44:01       21 阅读
  8. 风景园林工程设计乙级资质业绩要求案例分析

    2024-07-11 12:44:01       23 阅读