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) {

         vector<vector<int> > result;

         vector<int> tmp;

         sort(S.begin(),S.end());

         getSubset(result,S,,tmp);

         return result;

     }

     void getSubset(vector<vector<int> > &result,vector<int> &S,int index,vector<int> tmp)

     {

         if(index==S.size())

         {

             result.push_back(tmp);

             return;

         }

         getSubset(result,S,index+,tmp);

         tmp.push_back(S[index]);

         getSubset(result,S,index+,tmp);

     }

 };
 

【leetcode】Subsets的更多相关文章

  1. 【leetcode】Subsets (Medium) ☆

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

  2. 【leetcode】Subsets II (middle) ☆

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

  3. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  4. 【LeetCode】 Subsets

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

  5. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  6. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  7. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  8. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. hdu5007 字符串

    字符串问题.是否出现iPhone Apple等词:我考虑时想到既然是否有这些词,可以写map标记一下:然后又最长的是iPhone,6个单词,所以第一个for遍历所有单词 然后在一个for(1~6),用 ...

  2. php复习

    最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...

  3. 【BZOJ-3293&1465&1045】分金币&糖果传递×2 中位数 + 乱搞

    3293: [Cqoi2011]分金币 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 854  Solved: 476[Submit][Status] ...

  4. android中Handle类的用法

    android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...

  5. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  6. strstr()

    char * __cdecl strstr ( const char * str1, const char * str2 ) { char *cp = (char *) str1; char *s1, ...

  7. mysql zip 版本配置方法

    -\bin 指 C:\Program Files\MySQL\MySQL Server 5.6\bin 1.增加环境变量 "PATH"-"-\bin" 2.修改 ...

  8. 菲涅尔反射(Fresnel Reflection)

    离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...

  9. MSN

    msn (微软软件) 编辑 MSN,全称Microsoft Service Network,是微软公司(Microsoft)旗下的门户网站. MSN(门户网站)提供包括必应搜索.文娱.健康.理财.汽车 ...

  10. 普通用户如何临时获取root权限

    转自:http://634871.blog.51cto.com/624871/1325907 在实际工作中,公司不会将root用户直接给员工使用,而是通过员工自己的账号临时获得系统的root权限. 1 ...