C++ source code


  • 0
    N
    vector<vector<int>> build(vector<vector<int>> &ret, vector<int> &nums, int start)
    {
    	if (start >= nums.size())
    	{
    		ret.push_back(vector<int>());                      // pi. null.
    		return ret;
    	}
    
    	int cur = nums[start];
    	int next = start;
    	while (next<nums.size() && cur == nums[next]) next++;  // count the duplicates
    
    	build(ret, nums, next);                                // direction: right to left
    	int size = ret.size();
    	vector<vector<int>> rocknroll = ret;                   // copy the previous values
    
    	for (int i = 1; i <= next - start; i++)
    	{
    		for (int j = 0; j<size; j++) rocknroll[j].insert(rocknroll[j].begin(), cur); // insert a number in front of the previous values
    		for (int j = 0; j<size; j++) ret.push_back(rocknroll[j]); // insert the values after the previous values.
    	}
    
    	return ret;
    }
    
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
    	vector<vector<int>> ret;
    	sort(nums.begin(), nums.end());
    	return build(ret, nums, 0);
    }

Log in to reply
 

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