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,组合问题)的更多相关文章

  1. [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  4. Different Subsets For All Tuples CodeForces - 660E (组合计数)

    大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$ ...

  5. 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 ...

  6. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  7. HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. 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 ( ...

  9. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

随机推荐

  1. EditText自动弹出软键盘

    editText.requestFocus() editText.isFocusable = true editText.isFocusableInTouchMode = true val timer ...

  2. thinkphp查询,3.X 5.0 亲试可行

    [php] view plain copy   print? 一.介绍 ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取.更新和删除等操作,主要涉及到wher ...

  3. H.264学习笔记2——帧内预测

    帧内预测:根据经过反量化和反变换(没有进行去块效应)之后的同一条带内的块进行预测. A.4x4亮度块预测: 用到的像素和预测方向如图: a~f是4x4块中要预测的像素值,A~Q是临块中解码后的参考值. ...

  4. qt 5中文乱码

    头文件加上#prama execution_character_set("utf-8")

  5. (一)Redis for Windows正确打开方式

    目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 下载地址 官网.中文网1 及 中 ...

  6. leetcode_865. Smallest Subtree with all the Deepest Nodes

    https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树 ...

  7. Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

  8. 06CSS列表

    CSS列表 列表样式——list-style-type list-style-type:<属性值> disc   黑圆点 circle 空心圆点 square   小黑方块 decimal ...

  9. Handler和内部类的正确用法

    PS:本文摘抄自<Android高级进阶>,仅供学习使用 Android代码中涉及线程间通信的地方经常会使用Handler,典型的代码结构如下. public class HandlerA ...

  10. 第2节 mapreduce深入学习:6、MapReduce当中的计数器

    第2节 mapreduce深入学习:6. MapReduce当中的计数器 计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计.计数器还可辅助诊断系统故障.如果需要将日志信息传输到map ...