【leetcode】Subsets (Medium) ☆
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.
就是找出数字的全部子集。
我的思路:
设输入是 1 2 3 4
先把输入从小到大排序
长度 0 : []
长度 1 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 没有 直接压入 [1]
2,找上一个数字结果中开始数字比2大的解 没有 直接压入 [2]
...
得到长度为1的解是 [1] [2] [3] [4]
长度 2 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2] [3] [4], 压入[1 2][1 3][1 4]
2, 找上一个数字结果中开始数字比2大的解 有 [3] [4], 压入[2 3][2 4]
...
得到长度为2的解是 [1 2][1 3][1 4][2 3][2 4][3 4]
长度 3 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2 3][2 4][3 4], 压入[1 2 3][1 2 4][1 3 4]
2, 找上一个数字结果中开始数字比2大的解 有 [3 4], 压入[2 3 4]
得到长度为3的解是 [1 2 3][1 2 4][1 3 4][2 3 4]
长度 4 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2 3 4], 压入[1 2 3 4]
2, 找上一个数字结果中开始数字比2大的解 没有
得到长度为4的解是 [1 2 3 4]
代码用了posfirst,poslast来表示上一个长度答案的范围。
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int>> ans;
vector<int> partans;
ans.push_back(partans); //空的
int posfirst = ; //上一个长度子集在ans中的起始下标
int poslast = ; //上一个长度子集在ans中的结束下标
for(int len = ; len <= S.size(); len++) //对长度循环
{
poslast = ans.size() - ;
for(int i = ; i < S.size(); i++) //对起始数字循环
{
while(!ans[posfirst].empty() && S[i] >= ans[posfirst][] && posfirst <= poslast) //跳过上一个长度答案中起始数字小于等于当前起始数字的解
{
posfirst++;
}
for(int pos = posfirst;pos <= poslast; pos++) //获取当前的答案
{
partans.push_back(S[i]); //压入当前数字
for(int j = ; j < ans[pos].size(); j++) //压入上一个长度答案中的数字
{
partans.push_back(ans[pos][j]);
}
ans.push_back(partans);
partans.clear();
}
}
posfirst = poslast + ;
}
return ans;
}
};
大神的解法:https://oj.leetcode.com/discuss/9213/my-solution-using-bit-manipulation
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort (S.begin(), S.end());
int elem_num = S.size();
int subset_num = pow (, elem_num);
vector<vector<int> > subset_set (subset_num, vector<int>());
for (int i = ; i < elem_num; i++)
for (int j = ; j < subset_num; j++)
if ((j >> i) & )
subset_set[j].push_back (S[i]);
return subset_set;
}
};
解释如下:
This is an amazing solution.Learnt a lot.Let me try to explain this to those who didn't get the logic.
Number of subsets for { , , } = ^ .
why ?
case possible outcomes for the set of subsets
-> Take or dont take =
-> Take or dont take =
-> Take or dont take =
therefore , total = ** = ^ = { { } , {} , {} , {} , {,} , {,} , {,} , {,,} }
Lets assign bits to each outcome -> First bit to , Second bit to and third bit to
Take =
Dont take =
) -> Dont take , Dont take , Dont take = { }
) -> Dont take , Dont take , take = { }
) -> Dont take , take , Dont take = { }
) -> Dont take , take , take = { , }
) -> take , Dont take , Dont take = { }
) -> take , Dont take , take = { , }
) -> take , take , Dont take = { , }
) -> take , take , take = { , , }
In the above logic ,Insert S[i] only if (j>>i)& ==true { j E { ,,,,,,, } i = ith element in the input array }
element is inserted only into those places where 1st bit of j is
if( j >> & ) ==> for above above eg. this is true for sl.no.( j )= , , ,
element is inserted only into those places where 2nd bit of j is
if( j >> & ) == for above above eg. this is true for sl.no.( j ) = , , ,
element is inserted only into those places where 3rd bit of j is
if( j >> & ) == for above above eg. this is true for sl.no.( j ) = , , ,
Time complexity : O(n*^n) , for every input element loop traverses the whole solution set length i.e. ^n
【leetcode】Subsets (Medium) ☆的更多相关文章
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LeetCode】 Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
随机推荐
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- 让ie浏览器支持html5新标签的解决方法(使用html5shiv)
没估计错的话旧版浏览器都是不识别这些新增的标签所以都是用行内元素来处理解决的,所以,有一个解决办法的突破口就是让它变成块状元素就不会处于同一行了,这样在新旧浏览器都是可以显示同样的效果,再者就是让浏览 ...
- SandcastleBuilder-生成帮助文档的时候报错...
错误1: SHFB: Error BE0043: Unexpected error detected in last build step. See output above for details. ...
- hadoop之根据Rowkey从HBase中查询数据
1.Hbase 根据rowkey 查询 conf的配置信息如下: conf = new Configuration(); conf.set("hbase.zookeeper.quorum&q ...
- Sql统计一个字符串在另一个字符串出现的次数的函数-fnQueryCharCountFromString
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ),)) returns int as begin declare @pos int,@n int , ...
- CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...
- hdu5412——CRB and Queries
1.题目大意:区间第k大,单点修改 2.随便搞搞就好了= =,树套树或主席树,我写的很丑 #include <cstdio> #include <cstdlib> #inclu ...
- Linux文件读写机制及优化方式
导读 Linux是一个可控性强的,安全高效的操作系统.本文只讨论Linux下文件的读写机制,不涉及不同读取方式如read,fread,cin等的对比,这些读取方式本质上都是调用系统api read,只 ...
- tableview侧滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { ) { retu ...
- CSS 继承深度解析
FROM ME: 之前在研究前端性能优化的时候,就有学习关于CSS中“善用CSS中的继承”. 原文:CSS Inheritance, The Cascade And Global Scope: You ...