78. Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

class Solution {
public:
   
    void subset(vector<vector<int>>& result,vector<int>&path,int step,vector<int>&nums)
    {
        if(nums.size() == step)
        {
            result.push_back(path);
            return;
        }
       
        subset(result,path,step+1,nums);
        path.push_back(nums[step]);
        subset(result,path,step+1,nums);
        path.pop_back();
    }
   
   
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> result;
        if(nums.size() == 0)
        {
            return result;
        }
       
        vector<int> path;
        subset(result,path,0,nums);
        
        return result;
       
    }
};

时间: 2024-08-26 11:43:25

78. Subsets的相关文章

【Leetcode】78. Subsets(求集合的子集问题)

78. Subsets(求集合的子集问题) [分析]:求集合的所有子集问题.题目要求子集中元素非递减序排列,因此我们先要对原来的集合进行排序.原集合中每一个元素在子集中有两种状态:要么存在.要么不存在.这样构造子集的过程中每个元素就有两种选择方法:选择.不选择,因此可以构造一颗二叉树,例如对于例子中给的集合[1,2,3],构造的二叉树如下(左子树表示选择该层处理的元素,右子树不选择),最后得到的叶子节点就是子集:{ 链接 } 1 class Solution 2 { 3 private: 4 v

刷题78. Subsets

一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parentheses 算了,用最简单的回溯法吧: class Solution{ public: void dfs(vector<int>& nums,int start){ r.push_back(path); for(int i=start;i<len;i++){ path.push_ba

[leedcode 78] Subsets

Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【一天一道LeetCode】#78. Subsets

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not co

LeetCode 78. Subsets 20170606

Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 题目大意:给定一个整数集合,返回其所有子集的集合

LeetCode OJ 78. Subsets

Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2

leetCode 78.Subsets (子集) 解题思路和方法

Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2

[LeetCode] 78. Subsets 子集合

Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Python:

Leetcode 78. Subsets (backtracking) 90 subset

using prev class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] nums) { List<Integer> temp = new ArrayList<>(); back(temp,nums, 0); return res;