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
  • 相关阅读:
    软件对标分析
    第一阶段绩效评估
    自律小帮手:Alpha版使用说明
    团队 电梯演讲 原型展示
    意见评论
    Alpha版(内部测试版)发布
    意见汇总
    产品对比
    团队项目-第二阶段冲刺-3
    团队项目-第二阶段冲刺-2
  • 原文地址:https://www.cnblogs.com/superzrx/p/3337804.html
Copyright © 2011-2022 走看看