Subsets and Subsets II (回溯,DFS,组合问题)
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],
[]
] 该题和Combinations很类似,只不过是k需要从0到size中取值。
class Solution {
private:
vector<vector<int>> res;
vector<int> s;
public:
void tra(int k,int start,int dep,vector<int> temp)
{
if(dep==k){
res.push_back(temp);
return;
}
for(int i=start;i<s.size();++i){
temp.push_back(s[i]);
tra(k,i+,dep+,temp);//是i+1,而不是start+1
temp.erase(temp.end()-);
}
}
vector<vector<int>> subsets(vector<int> &S) {
s=S;
sort(s.begin(),s.end());
vector<int> temp;
for(int k=;k<=s.size();++k){
tra(k,,,temp);
}
return res;
}
};
我的分析图:

Subsets 2
Given a collection of integers that might contain duplicates, 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,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
class Solution {
private:
vector<vector<int>> res;
vector<int> s;
public:
void tra(int k,int start,int dep,vector<int> temp)
{
if(dep==k){
for (int i=;i<res.size();++i)
{
if(res[i]==temp) return;
}
res.push_back(temp);
return;
}
for(int i=start;i<s.size();++i){
temp.push_back(s[i]);
tra(k,i+,dep+,temp);
temp.erase(temp.end()-);
}
}
vector<vector<int>> subsetsWithDup(vector<int> &S) {
s=S;
sort(s.begin(),s.end());
vector<int> temp;
res.push_back(temp);
for(int k=;k<=s.size();++k){
tra(k,,,temp);
}
return res;
}
};
Subsets and Subsets II (回溯,DFS,组合问题)的更多相关文章
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- Different Subsets For All Tuples CodeForces - 660E (组合计数)
大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$ ...
- Path Sum II 总结DFS
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdoj--1027--Ignatius and the Princess II(dfs)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
随机推荐
- VMware Workstation安装CentOS 7和开发环境
VMware Workstation新建虚拟机 此处使用的是VMware Workstation 10,其安装过程即是常规Windos系统下软件安装方式,略过. 安装完成双击图标: 打开虚拟机主界面: ...
- SpringSecurity的简单使用
导入SpringSecurity坐标 在web.xml中配置过滤器 编写spring-securiy配置文件 编写自定义认证提供者 用户新增时加密密码 配置页面的login和logout 获取登录用户 ...
- idea 调试工具的使用
原文:https://blog.csdn.net/hao_hl1314/article/details/53120918 Intellij IDEA Debug调试区工具的使用方法 快捷键F9 ...
- PHP exif扩展方法开启详解(亲测)
本节主要介绍了如何开启PHP exif扩展方法,主要在于对php.ini文件的修改 服务器配置说明: 1.在php.ini文件中找到;extension=php_exif.dll,去掉前面的分号 2. ...
- Farseer.net轻量级开源框架 中级篇:探究ORM(Mapping)
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: SQL执行报告 下一篇:Farseer.net轻量级开源框架 中级篇: Cooki ...
- PHP流程控制考察点
php遍历数组的三种方法及各自的区别 php遍历数组的方式主要有三种: for循环 foreach循环 while.list().each()组合循环 其中: for循环只能遍历索引数组,foreac ...
- codeforces_B. Forgery
http://codeforces.com/contest/1059/problem/B 题意: For simplicity, the signature is represented as an ...
- 使用webpack+vue.js构建前端工程化
参考文章:https://blog.csdn.net/qq_40208605/article/details/80661572 使用webpack+vue.js构建前端工程化本篇主要介绍三块知识点: ...
- centos7下配置tomcat开机启动
配置tomcat的开机启动1> 在centos7的/etc/rc.d/rc.local中加入:(注意自己的路径)#java environment export JAVA_HOME=/usr/j ...
- 基于jQuery的用户界面插件集合---EasyUI
easyui是一种基于jQuery的用户界面插件集合.为创建现代化,互动,JavaScript应用程序,提供必要的功能.使用easyui你不需要写很多代码,你只需要通过编写一些简单HTML标记,就可以 ...