题目

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. macOS 将【允许从以下位置下载的应用】设置为:任意来源

    用管理员帐号进入Terminal: 1) 输入:sudo spctl --master-disable ,回车: 2) 重新进入该设置页面即可看到已生效:

  2. Centos6.8 搭建Lvs+Keepalived

    Keepalived keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层.第4层和第7层交换.Keepalived是自动完成,不需人工干涉. 简 ...

  3. Broken BST CodeForces - 797D

    Broken BST CodeForces - 797D 题意:给定一棵任意的树,对树上所有结点的权值运行给定的算法(二叉查找树的查找算法)(treenode指根结点),问对于多少个权值这个算法会返回 ...

  4. JAVA常用知识总结(三)——JAVA虚拟机

    先附一张JAVA虚拟机内存结构图: 其中JAVA虚拟机的线程问题<为什么JAVA虚拟机分为线程共享和非线程共享?>一文中已经有详细介绍,本文从面试中常问的一些JAVA虚拟机问题出发,主要从 ...

  5. Ionic之数据绑定ng-model

    ionic 完美的融合下一代移动框架,ionic 基于Angular语法,支持 Angularjs 的特性.但是我在开发的时候,遇到了坑.因为之后用的就是angularjs,so 理所当然的以为代码应 ...

  6. asp.net MVC 错误信息“没有为该对象定义无参数的构造函数”请求各位大神帮忙!

    在做一个登录的功能,没有用MVC自己生成的identity代码,仿照别人的代码写出了以后出现错误. 错误信息如下: 代码如下: 求各位asp.net大神支招,网上找了资料最终也没解决这个问题.

  7. maven编译报错 -source 1.5 中不支持 lambda(或diamond) 表达式,编码 UTF-8 的不可映射字符

    在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错  -source 1.5 中不支持 lambda 表达式. 错误原因: Maven Compiler 插件默认会加 -source ...

  8. Java并发——ThreadPoolExecutor线程池解析及Executor创建线程常见四种方式

    前言: 在刚学Java并发的时候基本上第一个demo都会写new Thread来创建线程.但是随着学的深入之后发现基本上都是使用线程池来直接获取线程.那么为什么会有这样的情况发生呢? new Thre ...

  9. 《Head First HTML与CSS》的HTML标签、属性

    一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...

  10. AlertDialog的几种用法

    xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...