Short Iterative C++ Solution


  • 1
    S
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<vector<int>> res {{}};
        sort(nums.begin(),nums.end());
        for (int i = 0; i < nums.size();) {
            int start = 0, size = res.size();
            do {
                for (int j = start; j < start + size; j++) {
                    vector<int> temp = res[j];
                    temp.push_back(nums[i]);
                    res.push_back(temp);
                }
                start += size;
            } while (++i > 0 && i < nums.size() && nums[i] == nums[i-1]);
        }
        return res;
    }

Log in to reply
 

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