subsets

作者: DaiMorph | 来源:发表于2019-06-27 00:54 被阅读0次
    class Solution {
    public:
        vector<vector<int>>ans;
        vector<int>path;
        vector<vector<int> > subsets(vector<int> &S) {
            sort(S.begin(),S.end());
            if(S.size()==0)return ans;
            dfs(S,0);
            sort(ans.begin(),ans.end(),comp);
            return ans;
        }
        void dfs(vector<int>S,int index)
        {
            if(index==S.size())
            {
                ans.push_back(path);
                return;
            }
            path.push_back(S[index]);
            dfs(S,index+1);
            path.pop_back();
            dfs(S,index+1);
        }
        static bool comp(vector<int>a,vector<int>b){
            if(a.size()<b.size())return true;
            else if(a.size()==b.size())return a<b;
            else return false;
        }
    };
    

    相关文章

      网友评论

          本文标题:subsets

          本文链接:https://www.haomeiwen.com/subject/gsskcctx.html