Subsets,Subsets II

一.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],
  []
]
class Solution {
public:
    void dfs(vector<int>& nums ,int numsSize,int startPos,vector<vector<int>>& res,vector<int>& oneOfRes)
    {
        for(int i=startPos;i<numsSize;i++){
            oneOfRes.push_back(nums[i]);
            res.push_back(oneOfRes);
            dfs(nums,numsSize,i+1,res,oneOfRes);
            oneOfRes.pop_back();
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> res;
        vector<int> oneOfRes;
        res.push_back(vector<int>());
        int numsSize = nums.size();
        dfs(nums,numsSize,0,res,oneOfRes);
        return res;
    }
};

二.SubsetsII

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 solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
class Solution {
public:
    void dfs(vector<int>& nums,int numsSize,int startPos,vector<vector<int>>& res,vector<int>& oneOfRes)
    {
        for(int i=startPos;i<numsSize;i++){
            if(i>startPos && nums[i]==nums[i-1]){
                continue;
            }
            oneOfRes.push_back(nums[i]);
            res.push_back(oneOfRes);
            dfs(nums,numsSize,i+1,res,oneOfRes);
            oneOfRes.pop_back();
        }
    }
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> res;
        vector<int> oneOfRes;
        int numsSize = nums.size();
        res.push_back(oneOfRes);
        dfs(nums,numsSize,0,res,oneOfRes);
        return res;
    }
};
时间: 2024-12-09 19:28:34

Subsets,Subsets II的相关文章

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1,2

leetcode -day31 Subsets I II

1.  Subsets Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [

42. Subsets &amp;&amp; Subsets II

Subsets Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1

LeetCode Subsets I&amp; II——递归

I Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3],

Leetcode | Subsets I &amp; II

Subsets I Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1,2

&amp;lt;LeetCode OJ&amp;gt; 78 / 90 Subsets (I / II)

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

[Swift]LeetCode78. 子集 | Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3] Output: [ [3],   [1],   [2],   [1,2,3],   [1,3],   [2,3],   [1,2],   [] ] 给

Subsets II [leetcode] 从获取子集的递归和循环方法说起,解决重复子集的问题

这一题和Permutation II很像,一个是排列一个是组合. 我们理清思路,从最基本的获取子集的方法开始分析: 获取子集总的来说可以用循环或者递归做,同时还可以根据数字对应的binary code得到. 例如s = {x,y,z}可以生成的组合有:x,y,z,xy,yz,xz,xyz,0 第一种思路: 1. 维护一个集合Set(i),包含s[0...i]可生成的所有组合 s[0...i+1]可以生成的所有组合为:Set(i) + (Set(i)+s[i+1]) void permutatio

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]