leetcode:78. 子集

学习要点

        首先就是要先明白什么是回溯:leetcode:46. 全排列-CSDN博客。在此基础上我们可以细化一下这种题的解题思路。

题目链接

        78. 子集 - 力扣(LeetCode)

题目描述

leetcode:78. 子集_第1张图片

解法:回溯

class Solution {
public:
    vector> ret;
    vector path;
    void dfs(vector& nums,int pos)
    {
        for(int i =pos;i> subsets(vector& nums) {
        ret.clear(); ret.push_back(vector());
        dfs(nums,0);
        return ret;
    }
};

解析

  1. 首先要了解回溯算法,上文附有链接
  2. 先添加子集中有v[0]的
  3. 再添加子集中有v[1]但是没有v[0]的
  4. 再添加子集中有v[2]但是没有v[0]和v[1]的
  5. 以此类推

你可能感兴趣的:(#,回溯,#,深搜,leetcode,算法,数据结构)