Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
解题思路:
1-n进行递归?
- class Solution {
- private:
- vector<vector<int>> ret;
- vector<int> store;
- void deep(int level, vector<vector<int>> &ret,vector<int>& nums,int count,int index){
- if(level == 0) {ret.push_back(store);return;}
- if(level == nums.size()) {ret.push_back(nums);return;}
- if(count == level){ret.push_back(store);return;}
- for(int i=index;i<nums.size();i++){
- store.push_back(nums[i]);
- deep(level,ret,nums,count+1,i+1);
- store.pop_back();
- }
- }
- public:
- vector<vector<int>> subsets(vector<int>& nums) {
- for(int i=0;i<=nums.size();i++){
- store.clear();
- deep(i,ret,nums,0,0);
- }
- return ret;
- }
- };