首页 > 代码库 > 078. Subsets

078. Subsets

 1 class Solution { 2 public: 3     vector<vector<int>> subsets(vector<int>& nums) { 4         sort(nums.begin(), nums.end()); 5         vector<vector<int>> result; 6         vector<bool> selected(nums.size(), false); 7         subsets(nums, selected, 0, result); 8         return result; 9     }10 private:11     void subsets(const vector<int>& s, vector<bool>& selected, int step, vector<vector<int>>& result)12     {13         if (step == s.size()) {14             vector<int> subset;15             for (int i = 0; i < step; ++ i) {16                 if (selected[i]) subset.push_back(s[i]);17             }18             result.push_back(subset);19             return;20         }21         else {22             subsets(s, selected, step + 1, result);23             selected[step] = true;24             subsets(s, selected, step + 1, result);25             selected[step] = false;26         }27     }28 };

 

078. Subsets