Given a set of distinct integers, nums, return all possible subsets.
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], [] ]
class Solution { public: vector<vector<int>> res; vector<vector<int>> subsets(vector<int>& nums) { vector<int> cur; BFS(nums,cur,0); return res; } void BFS(vector<int>& nums,vector<int>& cur,int k) { if(cur.size()==nums.size()) { res.push_back(cur); return; } else { res.push_back(cur); for(int i=k;i<nums.size();i++) { vector<int> temp=cur; temp.push_back(nums[i]); BFS(nums,temp,i+1); } } } };
原文:http://blog.csdn.net/u011391629/article/details/52125721