问题描述:

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) {
sort(nums.begin(),nums.end());
vector<int> v;
vector<vector<int>> res;
res.push_back(v);
for(int i=;i<nums.size();i++)
{
for(int j=res.size()-;j>=;j--)
{
v.assign(res[j].begin(),res[j].end());
v.push_back(nums[i]);
res.push_back(v);
}
}
return res;
}
};

方法二:递归

 class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
res.reserve(pow(,nums.size()));
vector<int> v;
subset(res,nums,,v);
return res;
}
private:
void subset(vector<vector<int>>& res,vector<int>& s,int start,vector<int>& v)
{
if(start == s.size())
{
res.push_back(v);
return;
}
subset(res,s,start+,v);
v.push_back(s[start]);
subset(res,s,start+,v);
v.pop_back();
}
};

leetcode5:subsets问题的更多相关文章

  1. Subsets II

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

  2. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  3. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  5. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  6. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. [Leetcode] Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  9. 42. Subsets && Subsets II

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

随机推荐

  1. Default style sheet for HTML 4

    http://www.w3.org/TR/CSS21/sample.html html, address, blockquote, body, dd, div, dl, dt, fieldset, f ...

  2. SQL去除重复记录

    SQL去除重复记录 if not object_id('Tempdb..#T') is null     drop table #T Go Create table #T([ID] int,[Name ...

  3. 2、Windows下安装配置Redis

    windows下redis软件开源安装包挂载到github上,下面将详细介绍如何在windows下安装redis服务器 下载地址:https://github.com/MSOpenTech/redis ...

  4. Java50道经典习题-程序19 输入行数打印菱形图案

    题目:根据用户输入的行数打印菱形图案,若用户传入的是为偶数则提示用户重新输入,例如输入数字7打印出如下菱形图案   *  *** ************ *****  ***   *分析:先把图形分 ...

  5. 【OCP-12c】2019年CUUG OCP 071考试题库(76题)

    76.View the exhibit and examine the description of the DEPARTMENTSand EMPLOYEEStables. The retrieve ...

  6. AVA + Spectron + JavaScript 对 JS 编写的客户端进行自动化测试

    什么是 AVA (类似于 unittest) AVA 是一种 JavaScript 单元测试框架,是一个简约的测试库.AVA 它的优势是 JavaScript 的异步特性和并发运行测试, 这反过来提高 ...

  7. “全栈2019”Java第一百一十一章:内部类可以被覆盖吗?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. django入门-表单-part4

    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6514113.html 完全翻译自官方文档 https://docs.djangoproje ...

  9. elasticsearch-5.1.1 安装的问题

    elasticsearch 5.1 安装过程中遇到了一些问题做一些记录. 问题一:警告提示 [2016-12-20T22:37:28,543][INFO ][o.e.b.BootstrapCheck ...

  10. combining-filters

    The previous two examples showed a single filter in use. In practice, you will probably need to filt ...