Seawolf 发表于 2019-9-17 02:11:44

leetcode 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 =
Output:
[
,
,
,
,
,
,
,
[]
]

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
      List<List <Integer> > res = new ArrayList();
      helper(0, nums, new ArrayList<Integer>(), res);
      return res;
    }
   
    public void helper(int index, int[]nums, List<Integer> cur, List<List<Integer>> res){
      
      
      res.add(new ArrayList<>(cur));
      for(int i = index; i < nums.length; i++){
            cur.add(nums);
            helper(i+1, nums, cur,res);
            cur.remove(cur.size() -1);
      }
    }
}
页: [1]
查看完整版本: leetcode 78. Subsets