Leetcode 90 Solution

This article provides solution to leetcode question 90 (subsets-ii)

https://leetcode.com/problems/subsets-ii

Solution

class Solution {
    vector<vector<int>> m_res;
public:
    void subsetsWithDup(vector<int>& nums, int k, vector<int>& curr)
    {
        if (k == nums.size())
        {
            m_res.push_back(curr);
            return;
        }

        int n = 1;

        for (int i = k + 1; i < nums.size(); i++)
        {
            if (nums[i] == nums[i - 1])
                n++;
            else
                break;
        }

        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j < i; j++)
                curr.push_back(nums[k]);

            subsetsWithDup(nums, k + n, curr);

            for (int j = 0; j < i; j++)
                curr.pop_back();
        }
    }

    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());

        vector<int> curr;
        subsetsWithDup(nums, 0, curr);
        return m_res;
    }
};