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代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
随机推荐
- Node.js——异步上传文件
前台代码 submit() { var file = this.$refs.fileUpload.files[0]; var formData = new FormData(); formData.a ...
- vb,wps,excel 提取括号的数字
Sub 抽离数字() Dim hang Range("h1").Select Columns("E:F").Select Selection.Clear Ran ...
- github修改仓库项目的语言类型
github是 采用Linguist来自动识别你的代码应该归为哪一类. 解决方法: 我们可以在仓库的根目录下添加.gitattributes文件: ## 使用 `.gitattributes` 配置文 ...
- Jmeter之WebService接口测试
一.简介 1.JMeter3.2前的版本,可以使用SOAP/XML-RPC Request插件直接进行webservice接口,而3.2后的版本则已经取消了这个接口,需要另外的方法才能进行测试. 2 ...
- Java SE、Java EE、Java ME 三者区别
现在一个个来分析 1. Java SE(Java Platform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 ...
- 简单说一下 TCP打洞和UDP打洞
1, TCP协议通信: 现在有两台电脑A和B.在 假设A的地址为 192.168.0.100 假设B的地址为 192.168.0.102 A想给B发送一个字符串Hello, 如果A,B之间采用TCP ...
- CAD参数绘制对齐标注(com接口)
主要用到函数说明: _DMxDrawX::DrawDimAligned 绘制一个对齐标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 第一条界线开始点X值 DOUBLE ...
- Java Servlet 非英文乱码
response.setHeader("Content-Type", "text/json; charset=UTF-8"); request.setChara ...
- C#面试问题及答案
1.遇到高并发的问题如何解决? 优化SQL语句 多线程 分布式服务器 集群 拆表2.Dictionary和ConurrentDictionary的区别? 后者是线程安全的 前者适用于单线程3.Dict ...
- css滚动条样式修改
.activeMoreBankList{ height: 188px; overflow-y: auto;} /*滚动条样式*/.activeMoreBankList::-webkit-scrollb ...