My c++ solution asking for some advice to my code


  • 0
    M
    void update(vector<vector<int> > &subset,vector<int> &set,int ele_num){
    	int subset_size = subset.size();
    	for(int i=0;i<subset_size;i++){
            //一定要取当前集合数量
    		int size = subset[i].size();
    		if(size!=ele_num)
    			continue;
    		else{
    			for(int j=0;j<set.size();j++){
    				if(set[j]>subset[i][size-1]){
    					vector<int> new_set = subset[i];
    					new_set.push_back(set[j]);
    					subset.push_back(new_set);
    				}
    			}
    		}
    	}
    }
    vector<vector<int> > get_subsets(vector<int> &S){
    	vector<vector<int> > subsets;
    	for(int i=0;i<S.size();i++){
            //初始化单元素子集
    		vector<int> temp(1,S[i]);
    		subsets.push_back(temp);
    	}
    	for(int j=1;j<S.size();j++){
            //按已有子集逐步更新
    		update(subsets,S,j);
    	}
    	vector<int> empty;
    	subsets.push_back(empty);
    	return subsets;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.