12ms c++


  • 0
    B
    class Solution {
    public:
        vector< vector<int> > ans;
        vector<int> num;
        vector<int> cnt;
        vector<int> tmp;
        int n;
        vector< vector<int> > subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            if (nums.size()==0)
            {
                ans.push_back(tmp);
                return ans;
            }
            num.push_back(nums[0]-1);
            cnt.push_back(0);
            n=0;
            for(int i=0;i<nums.size();i++)
            {
                if (nums[i]==num[n])
                    cnt[n]++;
                else
                {
                    num.push_back(nums[i]);
                    cnt.push_back(1);
                    n++;
                }
            }
            depthSearch(1);
            return ans;
        }
        void depthSearch(int index)
        {
            ans.push_back(tmp);
            for(int i=index;i<= n;i++)
            {
                if (cnt[i]==0) continue;
                tmp.push_back(num[i]);
                cnt[i]--;
                depthSearch(i);
                tmp.pop_back();
                cnt[i]++;
            }
            return ;
        }
    };
    

Log in to reply
 

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