• leetcode[90]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 subset(vector<vector<int>> &res,vector<int> &temp,vector<int> &S,int from)
    {
        res.push_back(temp);
        for(int i=from;i<S.size();i++)
        {
            if(i>from&&S[i]==S[i-1])continue;
            temp.push_back(S[i]);
            subset(res,temp,S,i+1);
            temp.pop_back();
        }
    }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            vector<vector<int>> res;
            res.clear();
            vector<int> temp;
            temp.clear();
            int n=S.size();
            if(n<1)return res;
            sort(S.begin(),S.end());
            subset(res,temp,S,0);
            return res;
        }
    /*    
    void subset(vector<vector<int>> &res,vector<int> &temp,vector<int> &S,int from)
    {
        if(from==S.size())
        {
            for(int i=0;i<res.size();i++)
            {
                if(res[i]==temp)return;
            }
            res.push_back(temp);
            return;
        }
        temp.push_back(S[from]);
        subset(res,temp,S,from+1);
        temp.pop_back();
        subset(res,temp,S,from+1);
    }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            vector<vector<int>> res;
            res.clear();
            vector<int> temp;
            temp.clear();
            int n=S.size();
            if(n<1)return res;
            sort(S.begin(),S.end());
            subset(res,temp,S,0);
            return res;
        }
    
    void subset(vector<vector<int>> &res,vector<int> &temp,vector<int> &S,int from, int to,int k)
    {
        if(k==0)
        {
            for(int i=0;i<res.size();i++)
            {
                if(res[i]==temp)return;
            }
            res.push_back(temp);
            return;
        }
        else
        {
            if(from>to)return;
            else
            {
                temp.push_back(S[from]);
                subset(res,temp,S,from+1,to,k-1);
                temp.pop_back();
                subset(res,temp,S,from+1,to,k);
                return;
            }
        }
    }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            vector<vector<int>> res;
            res.clear();
            vector<int> temp;
            temp.clear();
            int n=S.size();
            sort(S.begin(),S.end());
            if(n<1)return res;
            for(int k=0;k<=n;k++)
            {
                subset(res,temp,S,0,n-1,k);
            }
            return res;
        }
    */
    };
  • 相关阅读:
    每天一个linux命令(21):chgrp,chown,chmod
    设计模式之单例模式
    每天一个linux命令(20):find命令之exec
    每天一个linux命令(19):find 命令概览
    每天一个linux命令(18):locate 命令
    每天一个linux命令(17):whereis 命令
    【6折抢】戴尔i7新品Latitude高性能商用本
    Spring Cloud Gateway VS Zuul 比较,怎么选择?
    Zookeeper怎么实现分布式锁?
    数据库怎么分库分表,垂直?水平?
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281394.html
Copyright © 2020-2023  润新知