78. Subsets
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], [] ]
给定一组不同的整数nums,返回所有可能的子集
C++:
1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 vector<vector<int> > subs ; 5 vector<int> sub ; 6 subsets(nums,0,sub,subs) ; 7 return subs ; 8 } 9 10 void subsets(vector<int>& nums , int start , vector<int>& sub , vector<vector<int> >& subs) { 11 subs.push_back(sub) ; 12 for(int i = start ; i < nums.size() ; i++){ 13 sub.push_back(nums[i]) ; 14 subsets(nums,i+1,sub,subs) ; 15 sub.pop_back() ; 16 } 17 } 18 };