【LeetCode】78. Subsets (2 solutions)
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],
- []
- ]
解法一:
遍历S.size()位数的所有二进制数,1代表对应位置的元素在集合中,0代表不在。
一共2^n种情况。
详细步骤参照Subsets II
- class Solution {
- public:
- vector<vector<int> > subsets(vector<int> &S) {
- vector<vector<int> > result;
- int size = S.size();
- for(int i = ; i < pow(2.0, size); i ++)
- {//2^size subsets
- vector<int> cur;
- int tag = i;
- for(int j = size-; j >= ; j --)
- {//for each subset, the binary presentation has size digits
- if(tag% == )
- cur.push_back(S[j]);
- tag >>= ;
- if(!tag)
- break;
- }
- sort(cur.begin(), cur.end());
- result.push_back(cur);
- }
- return result;
- }
- };
解法二:
遍历所有元素,记当前元素为S[i]
遍历当前所有获得的子集,记为ret[j]
将S[i]加入ret[j],即构成了一个新子集。
详细步骤参照Subsets II
- class Solution {
- public:
- vector<vector<int> > subsets(vector<int> &S) {
- sort(S.begin(), S.end());
- vector<vector<int> > ret;
- vector<int> empty;
- ret.push_back(empty);
- for(int i = ; i < S.size(); i ++)
- {
- int size = ret.size();
- for(int j = ; j < size; j ++)
- {
- vector<int> newset = ret[j];
- newset.push_back(S[i]);
- ret.push_back(newset);
- }
- }
- return ret;
- }
- };
【LeetCode】78. Subsets (2 solutions)的更多相关文章
- 【LeetCode】78. Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【一天一道LeetCode】#78. Subsets
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【medium】78. Subsets
求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- 【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 ...
- 【LeetCode】46. Permutations (2 solutions)
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
随机推荐
- PHP 基础函数(三)数组和变量之间的转换
extract($arr);用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值注:(第二个参数很重要,可以看手册使用)使用方法 echo $a;compact(var1,var ...
- POJ 3525 Most Distant Point from the Sea (半平面交+二分)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3476 ...
- 配置druid内置的log实现
Druid不依赖任何的log组件,但支持多种log组件,会根据检测当前环境,选择一种合适的log实现. log的优先顺序 log4j -> log4j2 -> slf4j -> co ...
- windows及linux下安装django simple captcha 遇到的各种问题及解决的方法
转载自http://www.cnblogs.com/descusr/p/3225874.html 全部程序写完之后,验证码图片不显示,点击图片地址会提演示样例如以下错误,而且在linux下的纠正办法 ...
- Visual Studio IDE 背景色该为保护眼睛色
将背景颜色改成你想要的背景颜色. 将色调改为:85.饱和度:123.亮度:205->添加到自定义颜色->在自定义颜色选定点确定 就搞定了!
- [IIS]由安装IIS和.net framework先后顺序引发的问题,你中招了吗?
引言 最近帮别人做了一个小网站,在本机部署测试的时候,竟然浏览不了aspx后缀的页面,但可以浏览html页面,由此想到了IIS对静态页和动态页不同的处理方式. http请求到达服务器 当服务器接收到一 ...
- 让mbox支持up效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 周赛 POJ 3934 Queue
Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...
- 高级需求分析UML建模设计模式笔记
1.REQ->HLR 分析 全系统性质->AD设计 Context,BOM,Conception 2.REQ->LLR 分析 模块分析->DD设计 + 编码 Feature,B ...
- Android:手把手带你深入剖析 Retrofit 2.0 源码
前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将手把手带你深入剖析Retrofit v2.0的源码,希望你们会喜 ...