[Leetcode] Subsets II

Source: Internet
Author: User

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:


Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.

For example,
If S = [1, 2], a solution is:

[
[2],
[1],
[1, 2],
[2, 2],
[1, 2],
[]
]
This once made me catch chicken very much... Why is a bad dfs used this time? It is a one-time pass, and it is a large set of 80 ms .... Too Ironic...

class Solution {    set<vector<int>> result;public:    void dfs(vector<int>&S, int i, vector<int> tmp){        if(i == S.size()){            sort(tmp.begin(), tmp.end());            result.insert(tmp);            return;        }        dfs(S, i+1, tmp);                tmp.push_back(S[i]);        dfs(S, i+1, tmp);                    }        vector<vector<int> > subsetsWithDup(vector<int> &S) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        result.clear();        vector<int> tmp;        dfs(S, 0, tmp);        set<vector<int>>::iterator it;                vector<vector<int>> ret;        for(it = result.begin(); it!=result.end(); it++){            ret.push_back(*it);        }                return ret;           }};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.