Very Simple and Clear C++ Solution.


  • 0
    F
    class Solution {
        vector<vector<int>> results;
        void generate(vector<int>& nums, int k, vector<int> &result, bool isput)
        {
            if (k == nums.size())
                return results.push_back(result);
            if (k == 0 || nums[k] != nums[k - 1] || isput)
            {
                result.push_back(nums[k]);
                generate(nums, k + 1, result, true);
                result.pop_back();
            }
            generate(nums, k + 1, result, false);
        }
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<int> result;
            generate(nums, 0, result, false);
            return results;
        }
    };
    

Log in to reply
 

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