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. (一)Spring之初了解

    1.认识 Spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AO ...

  2. 微信小程序开发初次尝试-----实验应用制作(一)

    初次尝试微信小程序开发,在此写下步骤以做记录和分享. 1.在网上找了很多资料,发现这位知乎大神提供的资料非常全面. 链接 https://www.zhihu.com/question/50907897 ...

  3. sql server 2008 r2 无法定位到数据库文件目录

    像这样,选择数据库文件时, 无法定位到文件夹目录,子目录下的都不显示.明明选择的这个文件夹里还有很多子文件夹,却显示不了. 解决方法: 在此文件夹上右击,属性-安全 添加红框中的用户就可以了.

  4. php从mysql数据库中取数据

    php从数据库中取数据  面向过程 <?php $server_name="localhost:3306"; //数据库服务器名称 $username="root& ...

  5. SpringMVC 控制器统一异常处理

    摘要介绍spring mvc控制器中统一处理异常的两种方式:HandlerExceptionResolver以及@ExceptionHandler:以及使用@ControllerAdvice将@Exc ...

  6. python3安装opencv及电子书籍(百度云)

    不能直接  pip install opencv 正解: pip install opencv-python  记得:请确保网络良好!!!!! (1)这个是我学习的电子书籍:opencv-python ...

  7. OracleService類

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  8. CAD参数绘制填充(网页版)

    填充是CAD图纸中不可或缺的对象,在机械设计行业,常常需要将零部件剖开,以表现其内部的细节,而这些被剖开的截面会用填充来表示:在工程设计行业,一些特殊的材料或地形,也会用填充来表示. js中实现代码说 ...

  9. CAD嵌套打印(com接口版)

    当用户需要打印两个CAD控件的图纸时,可以采用嵌套打印实现.实现嵌套打印功能,首先将两个CAD控件放入网页中,C#代码如下: private void BatchPrintDialog() { MxD ...

  10. gym100825G. Tray Bien(轮廓线DP)

    题意:3 * N的格子 有一些点是坏的 用1X1和1X2的砖铺有多少种方法 题解:重新学了下轮廓线 写的很舒服 #include <bits/stdc++.h> using namespa ...