【Subsets】cpp

题目:

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:
    vector<vector<int>> subsets(vector<int>& nums) {
            const int len = nums.size();
            std::sort(nums.begin(), nums.end());
            vector<vector<int> > ret = Solution::subsubsets(nums, 0, len-1);
            vector<int> none;
            ret.push_back(none);
            return ret;
    }
    static vector<vector<int> > subsubsets(vector<int>& nums, int begin, int end)
    {
            vector<vector<int> > ret;
            if ( begin>end ) return ret;
            for ( int i = begin; i <=end; ++i )
            {
                // puts the curr value in
                vector<int> curr;
                curr.push_back(nums[i]);
                ret.push_back(curr);
                // get the subset
                vector<vector<int> > subset = Solution::subsubsets(nums, i+1, end);
                for ( int j = 0; j < subset.size(); ++j )
                {
                    //ret.push_back(subset[j]);
                    subset[j].insert(subset[j].begin(), nums[i]);
                    ret.push_back(subset[j]);
                }
            }
            return ret;
    }
};

tips:

每层递归的任务是活的传入数组的全部子集

1. 留出第i个元素

2. 把i+1到end的元素送到下一层递归

3. 递归终止条件是begin>end

返回下一集全部子集后,算上第i个元素后的全部集合如下:

1)第i个元素单独算一个

2)第i个元素 + 子集中每个元素

这样递归到第0个元素,就得到了全部的非空子集;再根据题目要求补上空集。

============================================

完成之后觉得好像哪里不对。{1,2,3} 如果留下了1传入{2,3}时,这个过程中已经获得了{2,3}全部子集。

修改后的代码如下:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
            const int len = nums.size();
            std::sort(nums.begin(), nums.end());
            vector<vector<int> > ret = Solution::subsubsets(nums, 0, len-1);
            vector<int> none;
            ret.push_back(none);
            return ret;
    }
    static vector<vector<int> > subsubsets(vector<int>& nums, int begin, int end)
    {
            vector<vector<int> > ret;
            if ( begin>end ) return ret;
            // puts the curr value in
            vector<int> curr;
            curr.push_back(nums[begin]);
            ret.push_back(curr);
            // get the subset
            vector<vector<int> > subset = Solution::subsubsets(nums, begin+1, end);
            for ( int j = 0; j < subset.size(); ++j )
            {
                ret.push_back(subset[j]);
                subset[j].insert(subset[j].begin(), nums[begin]);
                ret.push_back(subset[j]);
            }
            return ret;
    }
};

tips:

这个版本整理了逻辑误区。

时间: 2024-08-02 07:01:33

【Subsets】cpp的相关文章

【Combinations】cpp

题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 代码: class Solution { public: vector<vector<int>> combine

【Anagrams】 cpp

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 代码: class Solution { public: vector<string> anagrams(vector<string>& strs) { vector<string> ret; map<string,vec

【Triangle 】cpp

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

【N-Queens】cpp

题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration

【Permutations】cpp

题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 代码: class Solution { public: vector<vector<int>> permute(vector&

【3Sum】cpp

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

【4Sum】cpp

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or

【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法

/lib/cpp fails sanity check的解决 在某些软件的时候,运行./configure 会报错,错误提示为: configure: error: C++ preprocessor “/lib/cpp” fails sanity  check See `config.log’ for more details 解决办法:出现该情况是由于c++编译器的相关package没有安装,以root用户登陆,在终端上执行: # yum install glibc-headers # yum

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector