题目:
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], [] ]
代码:
class Solution { public: void backSubsets(set<vector<int>>& res, vector<int> nums) { if(nums.size() < 1) return; if(res.count(nums)) return; res.insert(nums); for(int i = 0; i<nums.size(); i++) { vector<int> cur_nums = nums; cur_nums.erase(cur_nums.begin()+i); backSubsets(res, cur_nums); } return; } vector<vector<int>> subsets(vector<int>& nums) { set<vector<int>> res; vector<int> cur; res.insert(cur); backSubsets(res, nums); vector<vector<int>> results; for(set<vector<int>>::iterator iter = res.begin(); iter != res.end(); iter++) results.push_back(*iter); return results; } };