10ms c++ solution


  • 0
    Z

    my c++ 10ms solution

    	static bool bigger(int i, int j)
    {
    	return (i > j);
    }
    
    vector<vector<int>> get_subsets(vector<int> &data, int pos)
    {
    	vector<vector<int>> ret;
    	vector<int> t;
    
    	if (pos >= data.size())	return ret;
    
    	// 递归调用
    	vector<vector<int>> next_ret = get_subsets(data, pos + 1);
    	
    	t.push_back(data[pos]);
    	ret.push_back(t);
    
    	for (vector<vector<int>>::iterator it = next_ret.begin(); it != next_ret.end(); ++it){
    		t = *it;
    		ret.push_back(t);
    
    		t.push_back(data[pos]);
    		ret.push_back(t);
    	}
    
    	return ret;
    }
    
    vector<vector<int>> subsets(vector<int> &S) 
    {
    	// 排序
    	sort(S.begin(), S.end(), bigger);
    
    	// 递归调用
    	vector<vector<int>> ret =  get_subsets(S, 0);
    	vector<int> t;
    	
    	ret.push_back(t);
    	return ret;
    }

Log in to reply
 

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