Q9.4 Write a method that returns all subsets of a set.
2015-01-25 14:27
369 查看
Q:Write a method that returns all subsets of a set.
A:DFS.
A:DFS.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void dfs(const vector<int> &s, int start, vector<int> &path, vector<vector<int> > &res) { if (start == s.size()) { res.push_back(path); return ; } for (int i = start; i < s.size(); i++) { path.push_back(s[i]); dfs(s, i+1, path, res); path.pop_back(); } res.push_back(path); return ; } vector<vector<int> > subsets(vector<int> &A) { vector<vector<int> > res; if (A.size() == 0) { return res; } vector<int> cur; dfs(A, 0, cur, res); return res; } void printResult(vector<vector<int> > &res) { for (int i = 0; i < res.size(); i++) { cout<<"["; for (int j = 0; j <res[i].size(); j++) { cout<<res[i][j]; if (j != res[i].size()-1) { cout<<","; } } cout<<"]"<<endl; } } void dfs2(const vector<int> &s, int start, vector<int> &path, vector<vector<int> > &res) { if (start == s.size()) { res.push_back(path); return ; } int prev = s[start]+1; for (int i = start; i < s.size(); i++) { if (prev == s[i]) { continue; } prev = s[i]; path.push_back(s[i]); dfs2(s, i+1, path, res); path.pop_back(); } res.push_back(path); return ; } //如果存在重复元素 vector<vector<int> > subsetsWithDup(vector<int> &S) { sort(S.begin(), S.end()); vector<vector<int> > res; vector<int> path; dfs2(S, 0, path, res); return res; } int main() { vector<int> A; for (int i = 1; i <= 3; i++) { A.push_back(i); } vector<vector<int> > res = subsets(A); printResult(res); cout<<endl; A[2] = 2; res = subsetsWithDup(A); printResult(res); return 0; }
相关文章推荐
- Write a method that returns all subsets of a set.
- Algorithm_Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- Print all possible strings of length k that can be formed from a set of n characters
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
- Write a method to compute all permutations of a string
- Q9.5 Write a method to compute all permutations of a string
- LeetCode Online Judge:Given a set of distinct integers, S, return all possible subsets.
- Write a method that counts the number of occurrences of a given character in a string
- Ch8.3: find all the subsets of a set
- 输出一个集合所有子集的元素和(Print sums of all subsets of a given set)
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
- Dynamics CRM2016 Set Values of all Data Types using Web API
- Ruby: How to find all indices of elements that match a given condition?
- Javascript Object Method Properties console.log View all methods and properties of the object
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- Dynamics CRM2016 Set Values of all Data Types using Web API
- UnsupportedOperationException:setProperty must be overridden by all subclasses of SOAPMessage解决方法>有效