LeetCode OJ:Subsets II

2014-11-24 08:21:27 · 作者: · 浏览: 0

Subsets II

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,2], a solution is:

    [
      [2],
      [1],
      [1,2,2],
      [2,2],
      [1,2],
      []
    ]

    算法思想:

    递归求解,按照答案要求先要排序

    class Solution {
    public:
        void dfs(vector
        
          &S,int start,vector
         
           &path,vector
          
           > &result){ result.push_back(path); for(int i=start;i
           
             > subsetsWithDup(vector
            
              &S) { vector
             
              > result; vector
              
                path; sort(S.begin(),S.end()); dfs(S,0,path,result); return result; } };