[LeetCode] Subsets (bfs的vector实现)
2014-08-15 15:59
344 查看
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 =
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 = 0,end = 1; while(start < end) { temp = res[start]; temp0 = temp; start++; int n = 0; for(int i=0;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 - Array - 380. Insert Delete GetRandom O(1)(vector+map实现set)
- 【leetcode】104. Maximum Depth of Binary Tree【java】三种实现方法:递归、BFS、DFS
- leetcode 【 Subsets 】python 实现
- LeetCode 78 Subsets (Python详解及实现)
- LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth
- 【LeetCode-面试算法经典-Java实现】【078-Subsets(子集)】
- C++Templates中的例子: 用vector实现一个栈Stack类模板
- 有向图的DFS和BFS算法实现
- 深度遍历(DFS)与广度遍历(BFS) (C语言实现)
- vector的实现
- Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
- 使用vector实现矩阵,并计算N阶矩阵的行列式
- 【每天算法3】:用java语言实现,二维Vector的排序组合
- STL源码剖析中vector的Insert实现部分要点纪要
- struct+vector实现树结构
- openlayers中利用vector实现marker的方式
- [C++]vector嵌套实现2D数组
- 实现类的序列化--例子将Vector对象压入标准流
- 个人研究《数据结构与算法分析-C++描述》Vector实现的问题,new与初始化
- 使用vector实现矩阵,并计算N阶矩阵的行列式