zoukankan      html  css  js  c++  java
  • [leetcode]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],
      []
    ]
    
    这个曾经让我很捉鸡的题目。。。为什么这次用了个很烂的dfs,一次性通过,而且是80ms过大集合。。。。太讽刺了。。。

    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;
           
        }
    };



  • 相关阅读:
    JS新API标准 地理定位(navigator.geolocation
    微信公众号菜单
    js选择权
    mui 弹框
    又拍云
    弹框
    sublime插件
    将Apache的.htaccess转换到nginx中
    php 图片上传类
    C# 使用Com组件正确的释放方法
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3186998.html
Copyright © 2011-2022 走看看