题目:

题解:

Solution 1 ()

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res{{}};
sort(S.begin(), S.end());
for (int i = ; i < S.size(); ++i) {
int size = res.size();
for (int j = ; j < size; ++j) {
vector<int> instance = res[j];
instance.push_back(S[i]);
res.push_back(instance);
}
}
return res;
}
};

Solution 1.2

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res(, vector<int>());
sort(S.begin(), S.end()); for (int i = ; i < S.size(); i++) {
int n = res.size();
for (int j = ; j < n; j++) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
} return res;
}
};

Solution 2 ()

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res;
vector<int> v;
sort(S.begin(), S.end());
dfs(res, S, v, );
return res;
}
void dfs(vector<vector<int> > &res, vector<int> S, vector<int> &v, int pos) {
res.push_back(v);
for (int i = pos; i < S.size(); ++i) {
v.push_back(S[i]);
dfs(res, S, v, i + );
v.pop_back();
}
}
};

Bit Manipulation

This is the most clever solution that I have seen. The idea is that to give all the possible subsets, we just need to exhaust all the possible combinations of the numbers. And each number has only two possibilities: either in or not in a subset. And this can be represented using a bit.

There is also another a way to visualize this idea. That is, if we use the above example, 1 appears once in every two consecutive subsets, 2 appears twice in every four consecutive subsets, and 3 appears four times in every eight subsets, shown in the following (initially the 8 subsets are all empty):

[], [], [], [], [], [], [], []

[], [1], [], [1], [], [1], [], [1]

[], [1], [2], [1, 2], [], [1], [2], [1, 2]

[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]

Solution 3 ()

class Solution {
public:
vector<vector<int>> subsets(vector<int>& S) {
sort(S.begin(), S.end());
int num_subset = pow(, S.size());
vector<vector<int> > res(num_subset, vector<int>()); for (int i = ; i < S.size(); i++) {
for (int j = ; j < num_subset; j++) {
if ((j >> i) & ) {
res[j].push_back(S[i]);
}
}
}
return res;
}
};

【Lintcode】017.Subsets的更多相关文章

  1. 【Lintcode】018.Subsets II

    题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...

  2. 【CF660E】Different Subsets For All Tuples 结论题

    [CF660E]Different Subsets For All Tuples 题意:对于所有长度为n,每个数为1,2...m的序列,求出每个序列的本质不同的子序列的数目之和.(多个原序列可以有相同 ...

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

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

  4. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

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

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

  6. 【medium】78. Subsets

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

  7. 【LeetCode】78. Subsets (2 solutions)

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

  8. 【Lintcode】074.First Bad Version

    题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...

  9. 【LeetCode】90.Subsets II

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

随机推荐

  1. easyNetq demo

    本demo包含一个类库,2个console程序 1.新建类库  MQHelper,控制台程序  consumer和proc ,控制台程序引用MQHelper 2.使用nuget安装easynwtq 和 ...

  2. 解决Oracle用户被锁定的方法

    解决Oracle用户被锁定的方法 1,cmd控制台: 使用sqlplus 命令:sqlplus sys/密码@ip/orcl as sysdba; 2,先设置具体时间格式,以便查看具体时间 SQL&g ...

  3. EasyDSS流媒体视频实时回传与录像管理解决方案

    一.背景 1.1 方案背景 随着互联网基础设施建设的不断完善和发展,带宽的不断提速,尤其是光纤入户,4G/5G/NB-IoT各种技术的大规模商用,视频在各行各业越来越受到重视,无论是传统的视频媒体转向 ...

  4. 发送邮件 Email(java实现)

    //发送邮件 private static void sendMail(String mail, String mailContext) { try { //获取文本中html的内容 并动态替换并显示 ...

  5. 九度OJ 1165:字符串匹配 (模式匹配)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1149 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中和短字符串的所有匹配,输出 ...

  6. django url匹配过程

    ROOT_URLCONF root URLconf module urlpatterns “include” other URLconf modules chops off whatever part ...

  7. 【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二

    协议 CP/IP路由技术第一卷 作    者 (美)多伊尔,(美)卡罗尔

  8. HTML5+ Android打包证书

    HBuilder默认App云端打包默认使用的是DCloud公用证书,其信息如下: MD5: 59:20:1C:F6:58:92:02:CB:2C:DA:B2:67:52:47:21:12 SHA1:B ...

  9. 20179209课后作业之od命令重写

    一.问题描述: 1 复习c文件处理内容 2 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 3. main与其他分开,制作静态库和动态库 4. 编写Makefi ...

  10. mysql一:操作数据库

    创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...