题目

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

分析

求给定一个集合的子集!

该题目与LeetCode 77 Combinations类似,都是动态规划思想的应用!

同理,不能采用暴力法解决问题,因为会带来很高的复杂度。

仔细分析,我们发现包含n个元素的集合的子集有以下三个部分组成:

  1. 第一部分,该集合前n-1个元素组成的集合的子集;
  2. 第二部分,该集合中最后一个元素构成的一个子集合;
  3. 第三部分,对该集合前n-1个元素组成的集合的子集,每一个都添加最后一个元素;

注意,对于返回的集合的全部子集包括一个空子集!

AC代码

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
if (nums.empty())
return vector<vector<int>>(); sort(nums.begin(), nums.end()); vector<vector<int>> ret = getSubsets(nums);
//不要丢掉最后的空子集
ret.push_back(vector<int>()); return ret;
} vector<vector<int>> getSubsets(vector<int>& nums) {
if (nums.empty())
return vector<vector<int>>();
int len = nums.size();
//初始化一个包含空元素的结果vector
vector<vector<int> > ret; //用0~len-1个原数组元素,建立一个新数组
vector<int> tem_nums(nums.begin(), nums.begin() + len - 1);
vector<vector<int>> tmp = getSubsets(tem_nums);
int t_size = tmp.size(); //第一部分,0~len-1所有元素的真子集
for (int i = 0; i < t_size; i++)
{
ret.push_back(tmp[i]);
}//for //第二部分,加入第len个元素
ret.push_back(vector<int>(1, nums[len - 1])); //第三部分,0~len-1所有元素的真子集,加入第len个元素
for (int i = 0; i < t_size; i++)
{
tmp[i].push_back(nums[len - 1]);
ret.push_back(tmp[i]);
}//for return ret;
}
};

GitHub测试程序源码

LeetCode(78) Subsets的更多相关文章

  1. LeetCode(78):子集

    Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...

  2. LeetCode(90) Subsets II

    题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. Hexo瞎折腾系列(3) - 添加GitHub彩带和GitHub Corner

    页面右上角添加GitHub彩带 你可以在这里找到一共12种样式的GitHub彩带,复制其中的超链代码. 在themes\next\layout\_layout.swig目录下找到头部彩带相关的代码: ...

  2. $.ajax从后台取数据 然后做字符串拼接的例子

  3. threading多线程模块

    1 基本实现 Thread(target=函数名,args=(以元组形式传递的实参,要加",")) th = threading.Thread(target=run,args=(i ...

  4. WOJ1019 所有的M数

    题目链接: WOJ1019 题目分析: 单调栈维护,读一个进来,如果前面的比它大就弹出来,然后压栈里(反正它在最右边) 压进栈里输出它前面那个数就好了 O(n)扫一遍就能过 真的水得不能再水的题了-- ...

  5. 洛谷 P2158 [SDOI2008]仪仗队 && 洛谷 P1447 [NOI2010]能量采集

    https://www.luogu.org/problemnew/show/P2158 以人所在位置为(0,0)建立坐标系, 显然除了(0,1)和(1,0)外,可以只在坐标(x,y)的gcd(x,y) ...

  6. centOS 部署服务器(一)

    接下来我所写的博客仅仅是为了记录我的学习过程,与其他无关. 由于公司换用了亚马逊服务器,用的是它的RDS数据库,所以就没有像以前的项目部署的时候使用mysql,不过要下载安装mysql-proxy,字 ...

  7. 127 Word Ladder 单词接龙

    给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则:    每次只能改变一个字母.    变换过程中的 ...

  8. hadoop-0.20.2伪分布式安装简记

    1.准备环境 虚拟机(redhat enterprise linux 6.5) jdk-8u92-linux-x64.tar.gz hadoop-0.20.2.tar.gz 2.关闭虚拟机的防火墙,s ...

  9. Spark MLlib编程API入门系列之特征选择之向量选择(VectorSlicer)

    不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). VectorSlicer用于从原来的特征 ...

  10. React-Native 开发问题整理

    1.内嵌WebView,点击输入框后页面不自动上滚 <activity android:name=".MainActivity" android:label="@s ...