zoukankan      html  css  js  c++  java
  • LeetCode-Subsets

    Given a set of distinct integers, 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,3], a solution is:

    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]
    
    注意空集
    class Solution {
    public:
        void Sub(vector<vector<vector<int> > >&total,vector<int>& a,int i){
            if(total[i].size()!=0){
                return;
            }
            else{
                if(i==0){
                    vector<int> one;
                    one.push_back(a[0]);
                    total[0].push_back(one);
                    total[0].push_back(vector<int>());
                    return;
                }
                else{
                    Sub(total,a,i-1);
                    vector<vector<int> >r1=total[i-1];
                    for(int j=0;j<r1.size();j++){
                        total[i].push_back(r1[j]);
                         r1[j].push_back(a[i]);
                        total[i].push_back(r1[j]);
                    }
                    return;
                }
            }
        }
        vector<vector<int> > subsets(vector<int> &S) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(S.size()==0)return vector<vector<int> >();
            sort(S.begin(),S.end());
            vector<vector<vector<int> > > total(S.size());
            Sub(total,S,S.size()-1);
            vector<vector<int> >& ret=total[S.size()-1];
            set<vector<int> > res;
            for(int i=0;i<ret.size();i++)res.insert(ret[i]);
            ret=vector<vector<int> >(res.begin(),res.end());
            return ret;
        }
    };
    View Code
  • 相关阅读:
    [Leetcode] 225. Implement Stack using Queues
    前端面试题2
    数据结构_stack
    数据结构 station
    数据结构_wow(泡泡的饭碗)
    数据结构_XingYunX(幸运儿)
    数据结构 nxd(顺序对)
    数据结构 hbb(汉堡包)
    数据结构 elegant_sequence(优雅的序列)
    数据结构 i_love(我喜欢)
  • 原文地址:https://www.cnblogs.com/superzrx/p/3337804.html
Copyright © 2011-2022 走看看