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],
[]
]

Summary: Recursive approach, we can optimize it.

     vector<vector<int> > subsets(vector<int> &S) {
std::sort(S.begin(), S.end());
vector<vector<int> > result;
vector<int> subset;
result.push_back(subset);
if(S.size() == )
return result;
for(int i = ; i < S.size(); i ++) {
vector<int> sub_s(S.begin() + i + , S.end());
vector<vector<int> > ret = subsets(sub_s);
for(auto item : ret){
item.insert(item.begin(), S[i]);
result.push_back(item);
}
} return result;
}

最新文章

  1. am等adb命令小总结
  2. CentOS6.4 配置HAProxy+Keepalived
  3. Spring 的微内核与FactoryBean扩展机制--转载
  4. C#封装加密算法(MD5、SHA、HMAC、DES、RSA)的一个类
  5. PHP之APC缓存详细介绍
  6. BZOJ 1009 GT考试
  7. 转:30分钟掌握STL
  8. PE结构之重定位表
  9. clear-fix清除浮动的两种写法
  10. 基于 Docker 的微服务架构实践
  11. react学习过程中遇到的错误记录
  12. MySql 查询表结构信息
  13. adb 安装安卓包
  14. drools规则引擎中易混淆语法分析_相互触发导致死循环分析
  15. 【百度杯】ctf夺旗大战,16万元奖励池等你拿
  16. celery 定时任务
  17. windows下Mysql8.0.12安装详解
  18. InfluxDB 常用命令
  19. 提取字符串substring()
  20. VMware Ubuntu安装

热门文章

  1. Datatable分页
  2. Ghostscript命令实践
  3. Beaglebone Black&ndash; 智能家居控制系统 LAS - 网页服务器 Node.js 、Web Service、页面 和 TCP 请求转 UDP 发送
  4. Linux链接库三(C跟C++之间动态库的相互调用)
  5. Windows Live Writer配置
  6. Nginx入门笔记之————配置文件结构
  7. new,delete和malloc,free以及allocator&lt;T&gt;
  8. CentOS6下yum下载的包存放路径
  9. 小度Wifi_设置
  10. mysql 检查字符串是否包含子串