码迷,mamicode.com
首页 > 其他好文 > 详细

078. Subsets

时间:2016-09-14 01:48:43      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

 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

标签:

原文地址:http://www.cnblogs.com/shadowwalker9/p/5870258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!