The topic of the law back Leetcode -78. Subset (Subsets)
Given a set of integer array elements no repeating the nums , which returns an array of all possible subsets (power set).
Description: Solution Set can not contain duplicate subsets.
Example:
Input: nums = [1,2,3] Output: [ [3], [1], [2], [2,3], [1,3], [2,3], [1,2 ], [] ]
Analysis:
This question is about a very basic question of backtracking, to an array of questions, ask him subset (including the empty set).
AC Code:
class Solution { List<List<Integer>> ans = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { int[] vis = new int[nums.length]; dfs(nums,new ArrayList<Integer>(),0); return ans; } public void dfs(int nums[],ArrayList<Integer> list,int step){ if(step==nums.length){ ans.add(new ArrayList<>(list)); return; } list.add(nums[step]); dfs(nums,list,step+1); list.remove(list.size()-1); dfs(nums,list,step+1); } }