A straightforward solution


  • 0
    1
    bool cmp(const vector<int>& v1, const vector<int>& v2) {
    	int len1 = v1.size(), len2 = v2.size();
    	if (len1 != len2) return len1 < len2;
    	for (int i = 0; i < len1; i++) {
    		if (v1[i] != v2[i]) return v1[i] < v2[i];
    	}
    	return true;
    }
    class Solution {
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(), nums.end());
    		vector<vector<int>> ret;
            vector<int> vec;
            int i, j, len = nums.size(), size = 1 << len;
    		for (i = 0; i < size; i++) {
            	vec.clear();
    			for (j = 0; j < len; j++) {
            		if ((i >> j) & 1) vec.push_back(nums[j]);
    			}
    			ret.push_back(vec);
    		}
    		sort(ret.begin(), ret.end(), cmp);
    		vector<vector<int>>::iterator it = unique(ret.begin(), ret.end());
    		ret.resize(distance(ret.begin(), it));
    		return ret;
        }
    };

Log in to reply
 

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