Simple C++ recursive solution


  • 1
    J
    class Solution {
    public:
    vector<vector<int> > subsets(vector<int> &S) {
    	vector<vector<int> >res;
    	vector<int> v;
    
    	sort(S.begin(), S.end());
    	subsets(res, S, v, 0);
    	return res;
    }
    
    void subsets(vector<vector<int> > &res, vector<int> &S, vector<int>& v, int level) {
    	res.push_back(v);
    
    	for (int i = level; i<S.size(); i++)
    	{
    		v.push_back(S[i]);
    		subsets(res, S, v, i+1);
    		v.pop_back();
    	}
      }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.