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:

  1. [
  2. [3],
  3. [1],
  4. [2],
  5. [1,2,3],
  6. [1,3],
  7. [2,3],
  8. [1,2],
  9. []
  10. ]
  1. class Solution {
  2. public:
  3. vector<vector<int> > subsets(vector<int> &S) {
  4. vector<vector<int> > res;
  5. int len = S.size();
  6. vector<int> temp0,temp;
  7. res.push_back(temp);
  8. int start = ,end = ;
  9.  
  10. while(start < end)
  11. {
  12. temp = res[start];
  13. temp0 = temp;
  14. start++;
  15.  
  16. int n = ;
  17. for(int i=;i<len;i++){
  18. if(find(temp.begin(),temp.end(),S[i])==temp.end()){
  19. temp.push_back(S[i]);
  20. sort(temp.begin(),temp.end());
  21. if(find(res.begin(),res.end(),temp)==res.end()){
  22. res.push_back(temp);
  23. n++;
  24. }
  25. temp = temp0;
  26. }//end if
  27. }//end for
  28. end += n;
  29. }//end while
  30. return res;
  31. }//end func
  32. };

[LeetCode] Subsets (bfs的vector实现)的更多相关文章

  1. LeetCode:Subsets I II

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

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  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 (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  7. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法

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

  9. Leetcode: Subsets & SubsetsII

    Subsets Description: Given a set of distinct integers, S, return all possible subsets. Note: Element ...

随机推荐

  1. POJ 1743 (后缀数组+不重叠最长重复子串)

    题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...

  2. 搭建你的第一个Django应用程序

    首先你要确保你机器上面安装了python:Python开发_python的安装 python的相关学习资料:http://www.cnblogs.com/hongten/tag/python/ 其次, ...

  3. 地球上最大的PHP站点 后端技术解密

    Facebook的扩展性挑战 在我们讨论细节之前,这里有一些Facebook已经做的软件规模: ◆Facebook有570000000000每月页面浏览量 (据Google Ad Planner) ◆ ...

  4. Leetcode | substr()

    求子串当然最经典的就是KMP算法了.brute force算法在leetcode上貌似也有一些技巧. brute force: char* StrStr(const char *str, const ...

  5. ImageMagick jmagick 安装

    在安装ImageMagick之前,请检查下面包已经安装 tiff-3.9.5.tar.gz         (rpm -qa|grep libtiff检查是否已经安装) libpng-1.2.46.t ...

  6. 【翻译】Kinect v2程序设计(C++) Color篇

    Kinect SDK v2预览版,获取数据的基本流程的说明.以及取得Color图像的示例程序的介绍. 上一节,是关于当前型号Kinect for Windows(后面称作Kinect v1)和次世代型 ...

  7. Sublime Text3注册码(可用)

    Sublime Text3注册码(可用) (2014-12-20 21:24:56) 转载▼ 标签: sublime sublime3 sublimetext sublimetext3 分类: 实用工 ...

  8. mysql 将null转代为0

    mysql 将null转代为0 分类: Mysql2012-12-15 11:56 6447人阅读 评论(1) 收藏 举报 1.如果为空返回0 select ifnull(null,0) 2.如果为空 ...

  9. Solr定时更新

    今天用到solr定时重建索引和增量更新技术,就从网上搜了一些资料,在这里给大家整理了一下,也经过了自己的测试,没有异常. Solr官方提供了很强大的Data Import Request Handle ...

  10. w win cmd VS broswer

    php -f Fetch data from db , use php without any bugs to analyse the data and read and write db, with ...