[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,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
public class Solution {
    //DFS
    //注意:本题是要所有的组合,所以跟全排列的不同是,深搜的判断条件是只要符合要求的就加入最终结果
    //注意递归函数中,getSub的参数意义:从start开始
    //注意需要排序,题目暗含递增排序
    //时间复杂度O(2^n),空间复杂度O(n)
    List<List<Integer>> res;
    List<Integer> seq;
    public List<List<Integer>> subsets(int[] nums) {
        res=new ArrayList<List<Integer>>();
        seq=new ArrayList<Integer>();
        Arrays.sort(nums);/////////
        getSub(nums,0,nums.length);
        return res;
    }
    public void getSub(int[] nums,int start,int end){
        if(start<=end){///////
            res.add(new ArrayList<Integer>(seq));

        }
        for(int i=start;i<end;i++){
            seq.add(nums[i]);
            getSub(nums,i+1,end);//////
            seq.remove(seq.size()-1);
        }
    }
}
时间: 2024-12-24 05:01:29

[leedcode 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

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: 

【一天一道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

[leedcode 90] Subsets II

Given a collection of integers that might contain duplicates, 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,2], a solutio

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