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:

  1. [
  2. [3],
  3. [1],
  4. [2],
  5. [1,2,3],
  6. [1,3],
  7. [2,3],
  8. [1,2],
  9. []
  10. ]

解法一:

遍历S.size()位数的所有二进制数,1代表对应位置的元素在集合中,0代表不在。

一共2^n种情况。

详细步骤参照Subsets II

  1. class Solution {
  2. public:
  3. vector<vector<int> > subsets(vector<int> &S) {
  4. vector<vector<int> > result;
  5. int size = S.size();
  6. for(int i = ; i < pow(2.0, size); i ++)
  7. {//2^size subsets
  8. vector<int> cur;
  9. int tag = i;
  10. for(int j = size-; j >= ; j --)
  11. {//for each subset, the binary presentation has size digits
  12. if(tag% == )
  13. cur.push_back(S[j]);
  14. tag >>= ;
  15. if(!tag)
  16. break;
  17. }
  18. sort(cur.begin(), cur.end());
  19. result.push_back(cur);
  20. }
  21. return result;
  22. }
  23. };

解法二:

遍历所有元素,记当前元素为S[i]

遍历当前所有获得的子集,记为ret[j]

将S[i]加入ret[j],即构成了一个新子集。

详细步骤参照Subsets II

  1. class Solution {
  2. public:
  3. vector<vector<int> > subsets(vector<int> &S) {
  4. sort(S.begin(), S.end());
  5. vector<vector<int> > ret;
  6. vector<int> empty;
  7. ret.push_back(empty);
  8. for(int i = ; i < S.size(); i ++)
  9. {
  10. int size = ret.size();
  11. for(int j = ; j < size; j ++)
  12. {
  13. vector<int> newset = ret[j];
  14. newset.push_back(S[i]);
  15. ret.push_back(newset);
  16. }
  17. }
  18. return ret;
  19. }
  20. };

【LeetCode】78. Subsets (2 solutions)的更多相关文章

  1. 【LeetCode】78. Subsets 解题报告(Python & C++)

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

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

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

  3. 【LeetCode】90. Subsets II (2 solutions)

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

  4. 【一天一道LeetCode】#78. Subsets

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【medium】78. Subsets

    求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...

  6. 【LeetCode】77. Combinations (2 solutions)

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

  7. 【LeetCode】90.Subsets II

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

  8. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  9. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. PHP 基础函数(三)数组和变量之间的转换

    extract($arr);用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值注:(第二个参数很重要,可以看手册使用)使用方法 echo $a;compact(var1,var ...

  2. POJ 3525 Most Distant Point from the Sea (半平面交+二分)

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3476   ...

  3. 配置druid内置的log实现

    Druid不依赖任何的log组件,但支持多种log组件,会根据检测当前环境,选择一种合适的log实现. log的优先顺序 log4j -> log4j2 -> slf4j -> co ...

  4. windows及linux下安装django simple captcha 遇到的各种问题及解决的方法

    转载自http://www.cnblogs.com/descusr/p/3225874.html 全部程序写完之后,验证码图片不显示,点击图片地址会提演示样例如以下错误,而且在linux下的纠正办法 ...

  5. Visual Studio IDE 背景色该为保护眼睛色

    将背景颜色改成你想要的背景颜色. 将色调改为:85.饱和度:123.亮度:205->添加到自定义颜色->在自定义颜色选定点确定   就搞定了!

  6. [IIS]由安装IIS和.net framework先后顺序引发的问题,你中招了吗?

    引言 最近帮别人做了一个小网站,在本机部署测试的时候,竟然浏览不了aspx后缀的页面,但可以浏览html页面,由此想到了IIS对静态页和动态页不同的处理方式. http请求到达服务器 当服务器接收到一 ...

  7. 让mbox支持up效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 周赛 POJ 3934 Queue

    Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...

  9. 高级需求分析UML建模设计模式笔记

    1.REQ->HLR 分析 全系统性质->AD设计 Context,BOM,Conception 2.REQ->LLR 分析 模块分析->DD设计 + 编码 Feature,B ...

  10. Android:手把手带你深入剖析 Retrofit 2.0 源码

    前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将手把手带你深入剖析Retrofit v2.0的源码,希望你们会喜 ...