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:
    vector<vector<int>> res;
    void backtrack(vector<int>& nums, vector<int>& path, int start) {
        res.push_back(path);
        for(int i = start; i < nums.size(); ++i) {
            path.push_back(nums[i]);
            backtrack(nums, path, i+1);
            path.pop_back();
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> path = vector<int>();
        backtrack(nums,path,0);
        return res;
    }
};

 




 1 class Solution {
 2     
 3     List<List<Integer>> res = new ArrayList<>();
 4     
 5     public List<List<Integer>> subsets(int[] nums) {
 6         Arrays.sort(nums);
 7         help(new ArrayList<>(), nums, 0);
 8         return res;
 9     }
10     private void help(List<Integer> temp,int[] nums,int index){
11         temp = new ArrayList<>(temp);
12         res.add(temp);
13         for(int  i= index;i<nums.length;i++){
14             temp.add(nums[i]);
15             help(temp,nums,i+1);
16             temp.remove(temp.size()-1);
17         }
18     }
19 }

 

相关文章:

  • 2021-07-18
  • 2021-11-03
  • 2021-11-01
  • 2021-07-02
  • 2021-12-20
  • 2021-09-09
猜你喜欢
  • 2021-12-26
  • 2022-03-02
  • 2021-07-30
  • 2021-08-18
相关资源
相似解决方案