Topic
Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must is 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,3], [+] , [2,3], [up ], []]
Link to original topic (point me)
Thinking of solving problems
Sub-collection issues. Give a set to find out all of its subsets. This question is also a combinatorial problem-the old idea: recursive plus cycle. The understanding of such a topic is to find a practical example, and then simulate a layer of recursion, a layer of uniform, hands-on understanding 2 times, this type of problem, basically there are ideas.
Code implementation
Class Solution {public: vector<vector<int> > Subsets (vector<int> &s) { vector< vector<int> > ret; Sort (S.begin (), S.end ()); Helper (0, S, vector<int> (), ret); return ret; } void helper (int start, const vector<int>& S, vector<int> part, vector<vector<int> >& ret { if (start = = S.size ()) return; if (start==0) Ret.push_back (part); for (int i=start; i<s.size (); ++i) { part.push_back (s[i]); Ret.push_back (part); Helper (i+1, S, part, ret); Part.pop_back ();}} ;
And another very similar topic for this question, subset Ii.
If you think this article has a harvest for you, please help the top.
In addition, I opened the public number- sharing the beauty of technology , I will not regularly share some of my learning things.You can search the public number:
Swalgeor scan the QR code below to follow me .
(Reprint article Please specify Source: http://blog.csdn.net/swagle/article/details/30219965)
[Leetcode] subsets [31]