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 ], []]
Class Solution {public:vector<vector<int> > Subsets (vector<int> &s) {int n = s.size ( ); Heap_sort (S); vector<vector<int>> Res; for (int k=0; k<=n; k++) {vector<int> temp (k,0); Get_res ( -1,0,k,s,res,temp); } return res; } void Get_res (int loc, int now, int k, vector<int>& s, vector<vector<int>>& Res, vector<i nt>& temp) {if (k = = Now) {res.push_back (temp); Return } int n = s.size (); for (int i=loc+1; i<=n-k+now; i++) {Temp[now] = s[i]; Get_res (i,now+1,k,s,res,temp); } return; } void Swap (vector<int>& s, int i, int j) {int t = s[i]; S[i] = S[j]; S[J] = t; } void Sink (vector<int>& s, int loc, int end) {while (loc<end) {int k=2*loc+1 ; if (k>end) return; if (K<end && s[k+1]>s[k]) k++; if (S[loc] > S[k]) return; Swap (S,K,LOC); loc = k; }} void Heap_sort (vector<int>& s) {int n = s.size ()-1; for (int i= (n-1)/2; i>=0; i--) sink (s,i,n); while (n>0) {swap (s,0,n); n--; Sink (s,0,n); } }};
Leetcode--subsets