class Solution { public: vector< vector<int> >ans; vector<int> temp; vector<vector<int> > subsets(vector<int>& S) { sort(S.begin(), S.end()); //排序得到从小到大 ans.push_back(temp); //放入空集 int n = S.size(); for(int i=1; i<=n; i++){ //i表示子集长度;控制子集长度 dp(S, i, 0); } return ans; } void dp(vector<int>& S, int len,int index){ if(temp.size() == len){ //temp达到规定长度即添加 ans.push_back(temp); return; } for(int i=index; i<S.size();i++){ //要加的这一位,使用循环尝试每个数;本层一定会加一位 temp.push_back(S[i]); dp(S, len, i+1); //加入当前后,那么从后面开始 temp.pop_back(); //弹出,使用循环尝试下一个数 } return; } };