42. Subsets && 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,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
思想: 顺序读,取前面的每个子集,把该位置数放后面作为新的子集。
class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        sort(S.begin(), S.end());
        vector<vector<int> > vec(1);
        for(size_t id = 0; id < S.size(); ++id) {
            int n = vec.size();
            while(n-- > 0) {
                vec.push_back(vec[n]);
                vec.back().push_back(S[id]);
            }
        }
        return vec;
    }
};

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], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]思想: 排序后,按照 1 的方法。但是若前面的数字与本数字相同,则只读取含有前面数字的每个子集,把自身放在后面作为一个新的子集。
class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        sort(S.begin(), S.end());
        vector<vector<int> > vec(1);
        size_t prePos, endTag;
        prePos = endTag = 0;
        for(size_t id = 0; id < S.size(); ++id) {
            if(id > 0 && S[id] != S[id-1]) endTag = 0;
            else endTag = prePos;
            size_t n = vec.size();
            prePos = n;
            while(n > endTag) {
                --n;
                vec.push_back(vec[n]);
                vec.back().push_back(S[id]);
            }
        }
        return vec;
    }
};
				
时间: 2024-12-17 19:05:11

42. 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], [

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

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],

&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],   [] ] 给

Interval Sum I &amp;&amp; II

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result l

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se