Write a method that returns all subsets of a set
2013-09-21 18:29
183 查看
分析:1、每次向集合A中添加一个元素x,集合A为部分元素的所有组合 2、拷贝A,记为A1,将A1中每个元素和x进行拼接,然后将生成的元素放入A中
void addAllOne(vector<int> &to,vector<int> &from){ for(int i=0;i<from.size();i++) to.push_back(from[i]); } void addAllTwo(vector<vector<int> > &to, vector<vector<int> > &from){ for(int i=0;i<from.size();i++) to.push_back(from[i]); } vector<vector<int> > getAllSets(vector<int> set,int index){ vector<vector<int> > allsubsets; if(set.size()==index){ vector<int> empty; allsubsets.push_back(empty); }else{ allsubsets=getAllSets(set,index+1); int item=set[index]; vector<vector<int> > moresubsets; for(int i=0;i<allsubsets.size();i++){ vector<int> newsubset; addAllOne(newsubset,allsubsets[i]); newsubset.push_back(item); moresubsets.push_back(newsubset); } addAllTwo(allsubsets,moresubsets); } return allsubsets; }
相关文章推荐
- Algorithm_Write a method that returns all subsets of a set.
- Q9.4 Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set.
- Ch8.3: find all the subsets of a set
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
- Print all possible strings of length k that can be formed from a set of n characters
- 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 sums of all subsets of a given set)
- 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 to compute all permutations of a string
- Write a method that counts the number of occurrences of a given character in a string
- UnsupportedOperationException:setProperty must be overridden by all subclasses of SOAPMessage解决方...
- Sample script that displays all of the users in a given SQL Server DB
- Write a Servlet Application that uses a cookie to track the number of times a user visits a web site
- 【待】1.4 Write a method to replace all spaces in a string with'%20'.
- Write a program that gives count of common characters presented in an array of strings..(or array of
- Sample script that displays all of the users in a given SQL Server DB
- [scalability] Find all documents that contain a list of words
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times