Subsets [LeetCode]
2024-08-27 01:41:31
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;
}
最新文章
- am等adb命令小总结
- CentOS6.4 配置HAProxy+Keepalived
- Spring 的微内核与FactoryBean扩展机制--转载
- C#封装加密算法(MD5、SHA、HMAC、DES、RSA)的一个类
- PHP之APC缓存详细介绍
- BZOJ 1009 GT考试
- 转:30分钟掌握STL
- PE结构之重定位表
- clear-fix清除浮动的两种写法
- 基于 Docker 的微服务架构实践
- react学习过程中遇到的错误记录
- MySql 查询表结构信息
- adb 安装安卓包
- drools规则引擎中易混淆语法分析_相互触发导致死循环分析
- 【百度杯】ctf夺旗大战,16万元奖励池等你拿
- celery 定时任务
- windows下Mysql8.0.12安装详解
- InfluxDB 常用命令
- 提取字符串substring()
- VMware Ubuntu安装
热门文章
- Datatable分页
- Ghostscript命令实践
- Beaglebone Black&ndash; 智能家居控制系统 LAS - 网页服务器 Node.js 、Web Service、页面 和 TCP 请求转 UDP 发送
- Linux链接库三(C跟C++之间动态库的相互调用)
- Windows Live Writer配置
- Nginx入门笔记之————配置文件结构
- new,delete和malloc,free以及allocator<;T>;
- CentOS6下yum下载的包存放路径
- 小度Wifi_设置
- mysql 检查字符串是否包含子串