• 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
  • 相关阅读:
    11组 团队展示
    11组Alpha冲刺4/6
    11组Alpha冲刺2/6
    11组Alpha冲刺3/6
    EF code first 分页显示
    多条件分页存储过程控制器写法
    UML的9种图
    C#设计模式(2)——简单工厂模式
    多条件分页存储过程PageCommon写法
    五分钟读懂UML类图
  • 原文地址:https://www.cnblogs.com/superzrx/p/3337804.html
Copyright © 2020-2023  润新知