首页 > 其他分享 >78. 子集

78. 子集

时间:2023-04-14 18:26:10浏览次数:35  
标签:return nums int back depth vector 子集 78

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

> 我的解法

class Solution {
private:
    void find_son_depth(vector<int> &nums,int index,int d,int depth){
        if(d > depth) return;
        if(d == depth){
            res.emplace_back(vec);
            return;
        }
        for(int i = index; i < nums.size(); i++){
            vec.emplace_back(nums[i]);
            find_son_depth(nums,i+1,d+1,depth);
            vec.pop_back();
        }        
    }
    void find_son(vector<int> &nums,int depth){
        if(depth == 0) return;
        for(int i = 1; i <= depth; i++){
            find_son_depth(nums,0,0,i);
        } 
    }
public:
    vector<vector<int>> res;
    vector<int> vec;
    vector<vector<int>> subsets(vector<int>& nums) {
        int len = nums.size();
        res.resize(1<<len);
        res.clear();
        vec.clear();
        res.emplace_back(vec);
        std::sort(nums.begin(),nums.end());
        find_son(nums,len);
        return res;
    }
};

> 标准解法

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& nums, int startIndex) {
        result.push_back(path); // 收集子集,要放在终止添加的上面,否则会漏掉自己
        if (startIndex >= nums.size()) { // 终止条件可以不加
            return;
        }
        for (int i = startIndex; i < nums.size(); i++) {
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        result.clear();
        path.clear();
        backtracking(nums, 0);
        return result;
    }
};

标签:return,nums,int,back,depth,vector,子集,78
From: https://www.cnblogs.com/lihaoxiang/p/17319207.html

相关文章

  • 78、混合模式—变亮组
    1、在抠图的时候,如果选择了【主体】进行快速抠图的话,那么可能有些地方是扣的不是很合心意的,就要用到钢笔工具重新来抠画,勾画完就按【Ctrl+回车】,然后右键【建立选区】—>【添加到选区】,就可以了......
  • POJ 1780 Code (欧拉回路+非递归版dfs)
    题目地址:POJ1780还是求序列的欧拉回路。只不过这题有两坑。第一坑是用数字来当点的话,会MLE,因为每个数字可以连10条边,100w条边会MLE,即使用vector也会TLE。这题可以用边来记录,对于n为1时直接输出,然后后面的,比如12,23这两个点就用边权值为123来表示这两个点,这样就把点和边的范围......
  • HDU 1878 欧拉回路 (并查集+欧拉回路)
    题目地址:HDU1878这个题要注意欧拉回路与欧拉通路的区别。在都保证连通性的前提下,欧拉回路要求每个点的度数都是偶数,而欧拉通路允许两个点的度数是奇数。所以这题用并查集判断连通性后判断下度数就可以了。代码如下:#include<iostream>#include<string.h>#include<math.h>#in......
  • day28| 93+78+90
    93.复原ip地址 题目简述:有效IP地址正好由四个整数(每个整数位于0到255之间组成,且不能含有前导0),整数之间用'.'分隔。例如:"0.1.2.201"和"192.168.1.1"是有效IP地址,但是"0.011.255.245"、"192.168.1.312"和"[email protected]"是无效IP地址。给定一个只包含数字......
  • 78、扣水果—钢笔工具
    1、用钢笔工具把桃子抠出来之后,然后添加蒙版2、shift+点击蒙版(关闭蒙版),然后再用钢笔扣下一个桃子,然后ctrl+回车关闭路径,  再打开关闭的蒙版,然后给关闭的路径填充白色,这样第二个桃子就出来了    ......
  • UVa 10783 Odd Sum (water ver.)
    10783-OddSumTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724Givenarange [a, b],youaretofindthesummationofalltheoddintegersinthisran......
  • UVa 10785 The Mad Numerologist (排序)
    10785-TheMadNumerologistTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=1726Numerologyisasciencethatisusedbymanypeopletofindoutamansper......
  • 洛谷P2415 集合求和(数学问题,使用集合子集求和公式)
    可以知道对于一个有n个数据的集合,其子集个数有2^n个至于证明可以这样理解,对于n个数据,其子集就是对数据进行组和,而对于每个位置上的数据,组合时仅有两种状态即有此数据或无此数据,也就是有两种可能,而对于n个数据,就有2^n种可能不妨设其中一个非空数据X,对于X,依据X可以将子集划分为两......
  • 【LeetCode回溯算法#extra01】集合划分问题【火柴拼正方形、划分k个相等子集、公平发
    火柴拼正方形https://leetcode.cn/problems/matchsticks-to-square/你将得到一个整数数组matchsticks,其中matchsticks[i]是第i个火柴棒的长度。你要用所有的火柴棍拼成一个正方形。你不能折断任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须使用一次。如......
  • PAC主成分分析__784手写特征案例
    fromsklearn.neighborsimportKNeighborsClassifierasKNNfromsklearn.decompositionimportPCAfromsklearn.ensembleimportRandomForestClassifierasRFCimportmatplotlib.pyplotaspltfromsklearn.model_selectionimportcross_val_scoreimportpandasas......