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

78. Subsets

时间:2018-09-04 23:23:34      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:possible   not   distinct   tor   NPU   恢复   The   dex   power   

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
C++:
 1 class Solution {
 2 public:
 3     void process(vector<int>& nums, vector<int>& path, int index, vector<vector<int> >&result){
 4         //终止条件,路径保存
 5         if (index == nums.size()){
 6             result.push_back(path);
 7             return;
 8         }
 9         process(nums, path, index+1, result);
10         path.push_back(nums[index]);
11         process(nums, path, index + 1, result);
12         //恢复上一级的path
13         path.pop_back();
14     }
15 
16     vector<vector<int>> subsets(vector<int>& nums) {
17         sort(nums.begin(), nums.end());
18         vector<vector<int> > result;
19         vector<int> path;
20         process(nums, path, 0, result);
21         return result;    
22     }
23 };

 

78. Subsets

标签:possible   not   distinct   tor   NPU   恢复   The   dex   power   

原文地址:https://www.cnblogs.com/duan-decode/p/9588908.html

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