题目:

题解:

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;
}
};

最新文章

  1. c#取得应用程序根目录
  2. R读取溢出的数据
  3. 滚轮事件js
  4. C++ UFunction({FLAG}) 宏 FLAG 解释笔记
  5. [公告]这里的博客将不再更新,最新博客请移步至blog.coderzh.com
  6. Javascript this 关键字
  7. (转)Jmeter内存溢出处理方式记录
  8. iOS开发笔记--宏定义的黑魔法 - 宏菜鸟起飞手册
  9. Android ViewDragHelper源码解析
  10. 《Programming WPF》翻译 第7章 1.图形基础
  11. Libreoffice汉化
  12. Android ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to register to receive intents
  13. Windows 路径问题
  14. FZU 2256 迷宫
  15. Quartz学习——Quartz大致介绍(一)
  16. eclipse导入项目之后有感叹号
  17. PL/SQL NOCOPY限制模式
  18. 一个C++程序中有多个cin输入的情况
  19. sql的日期和时间函数–date_format
  20. 工作时间看股票:采用Excel RTD技术获取和讯网的实时股票行情及深沪港最新指数

热门文章

  1. HTTP状态码中301与302的区别
  2. js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决
  3. python之脚本参数optparse
  4. Nginx负载均衡简易配置
  5. TP实例化模型的两种方式 M() D()
  6. 01 redis特点及安装使用
  7. JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程
  8. CPU接口练习 (仅以此程序证明 某个同学真的有毒!有毒!!!)
  9. php如何在原来的时间上加一天?一小时?
  10. Complete space 完备空间与柯西序列 巴拿赫空间与完备空间 完备空间与和希尔伯特空间 封闭closed与完备性complete