My easy C++ code for recursive solution with 10 lines,8ms.


  • 0
    E
    vector<vector<int>> subsets(vector<int>& nums) 
    {
    	sort(nums.begin(), nums.end());
    	int sz = nums.size();
    	if (sz < 1)return vector<vector<int>>(1, nums);
    	int temp = nums[0];
    	nums.erase(nums.begin());
    	vector<vector<int>>res = subsets(nums);
    	vector<vector<int>>fin(res);
    	for (vector<vector<int>>::size_type it = 0; it != res.size(); it++)
    		res[it].insert(res[it].begin(), temp);
    	copy(res.begin(), res.end(), inserter(fin, fin.begin()));
    	return fin;
    }

Log in to reply
 

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