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],
[]
]
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res;
int len = S.size();
vector<int> temp0,temp;
res.push_back(temp);
int start = ,end = ; while(start < end)
{
temp = res[start];
temp0 = temp;
start++; int n = ;
for(int i=;i<len;i++){
if(find(temp.begin(),temp.end(),S[i])==temp.end()){
temp.push_back(S[i]);
sort(temp.begin(),temp.end());
if(find(res.begin(),res.end(),temp)==res.end()){
res.push_back(temp);
n++;
}
temp = temp0;
}//end if
}//end for
end += n;
}//end while
return res;
}//end func
};

[LeetCode] Subsets (bfs的vector实现)的更多相关文章

  1. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  2. LeetCode Subsets II (DFS)

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

  3. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  7. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

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

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

  9. Leetcode: Subsets & SubsetsII

    Subsets Description: Given a set of distinct integers, S, return all possible subsets. Note: Element ...

随机推荐

  1. 数据库操作sql【转】

    Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname,T#) 课程表SC(S#,C#,score) 成绩表Teacher(T#,Tname) 教师表 问题:1. ...

  2. 【BZOJ】1192: [HNOI2006]鬼谷子的钱袋(水题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1192 看到题我就好像想起以前小学升学考数学的最后一题,将一条金块分割最少的部分,使得每一天都能够支付 ...

  3. 低调的华丽,Windows Server 2003 ... 写给厌倦了XP,但又纠结于vista/win7花哨的童鞋(转)

    发布于2001年10月25日的windows XP 距今已近8年 时间, 微软从没有一个操作系统能像XP那样  坚挺这么久,婚姻既有7年之痒,何况用了8年XP的广大 同学,但07年发布的vista似乎 ...

  4. hdu-acm steps Common Subsequence

    /*这道题是很明显的dp题,状态方程有点不大好想,也许是我刚刚接触dp的缘故吧.dp[i][j]表示字符串s1取前i个字符s2取前j个字符时最大公共子序列的大小,这样的如果s1[i]==s2[j],d ...

  5. hdu Oil Deposits

    因为这道题中给的数据比较小,所以可以直接用枚举的方式进行dfs搜索,每出现一个‘@’,就进行一次dfs的搜索,将所有相邻的‘@’全部变成‘*’. #include"iostream" ...

  6. VirtIE6

    VirtIE6--能在win7上直接运行的单文件 http://down.iefans.net/VirtIE6.rar

  7. PreparedStatement与Statement的区别

    PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...

  8. 在Excel中实现查询功能

    $sn = Read-Host -Prompt "请输入员工号|序列号|资产号" $xl = New-Object -ComObject "Excel.Applicati ...

  9. 【贴图】网友 snoopy 用《iHMI43 液晶模块》做的界面给大家看看

    请大家欣赏! iHMI43 4.3寸液晶模块购买地址: http://item.taobao.com/item.htm?id=20508376359

  10. Transform-style和Perspective属性

    transform-style属性 transform-style属性是3D空间一个重要属性,指定嵌套元素如何在3D空间中呈现.他主要有两个属性值:flat和preserve-3d. transfor ...