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) ☆的更多相关文章

  1. 【leetcode】Subsets II (middle) ☆

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

  2. 【leetcode】Subsets II

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

  3. 【leetcode】Subsets

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

  4. 【LeetCode】 Subsets

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

  5. 【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 ...

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

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

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

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

  8. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  9. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

随机推荐

  1. Ubuntu/Windows下利用“HIDAPI”库函数实现与Hid类USB设备通信

    一.背景: 最近在做的一个项目需要使用USB传递数据,对USB理解不是很深,USB的系统驱动编写则更是天方 夜谭,因此将设备配置成HID类设备成为了首选.其最大的好处在于,LINUX/Windows系 ...

  2. SQL中关于日期的常用方法

    mysql数据库: logintime >= STR_TO_DATE('$$START_TIME','%Y-%m-%d %H:%i:%s') AND logintime < STR_TO_ ...

  3. 解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法

    Linux下默认cp命令是有别名的(alias cp='cp -i'),无法在复制时强制覆盖,即使你用 -f 参数也无法强制覆盖文件,下面提供几个从网上找的Linux下cp命令覆盖的方法. 1)取消c ...

  4. umeng

    http://bbs.umeng.com/thread-5408-1-1.html 微博分享 http://dev.umeng.com/social/ios/operation#2_2 http:// ...

  5. 解决Button设置disabled后无法执行后台代码问题

    一.开始调式下面的程序,发现Button在js中设置disabled后无法执行后台代码(btnsave_Click)问题 <asp:Button ID="btnsave" r ...

  6. webpack 教程 那些事儿02-从零开始

    接着上篇我们有了最简单的安装了webpack的项目目录这节我们从零开始搭建一个简单的基于webpack的spa应用demo本节只说基础常用配置项,复杂后续讲解. 文章目录 1. 新建项目结构目录,如下 ...

  7. javascript基础07

    javascript基础07 1.节点 元素.childNodes : 属性 只读 属性 子节点列表集合 元素.childNodes 只包含子节点,不包含孙节点 DOM节点的类型有很多种,w3c标准有 ...

  8. Unity手游之路<二>Java版服务端使用protostuff简化protobuf开发

    http://blog.csdn.net/janeky/article/details/17151465 开发一款网络游戏,首先要考虑的是客户端服务端之间用何种编码格式进行通信.之前我们介绍了Unit ...

  9. @synthesize的正确使用方式

    @synthesize的正确使用方式 一. @synthesize的错误使用方式 类1和类2是继承关系, name是类1的属性 但是类2的实现里加入了@synthesize name = _name; ...

  10. 我所理解的cocos2dx自适配屏幕大小方案

    这里主要有两个点: 1.屏幕大小的设置,也就是手机窗口的大小,在各个手机上面或者平板上的屏幕的大小. 这个大小的设置就是代码里面的:glview->setFrameSize(width, hig ...