Given a set of distinct integers, nums, return all possible subsets.
Note:
- Elements in a subset must is in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3] , a solution is:
[ 3], [1], [2], [1,3], [+] , [2,3], [up ], []]
Problem-solving ideas: And on the very similar, modify the title code can be, Java implementation is as follows:
static public list<list<integer>> subsets (int[] nums) { list<list<integer>> List = new Arraylist<list<integer>> (); List.add (New arraylist<integer> ()); Arrays.sort (nums); for (int i=1;i<=nums.length;i++) dfs (list, nums.length, I, 0,nums,-1); return list; } static list<integer> alist = new arraylist<integer> (); static void Dfs (List<list<integer>> List , int n, int k, int depth,int[] Nums,int last) {if (depth >= k) {List.add (new arraylist<integer> (alist)); return;} for (int i = last+1; I <= n-k+depth; i++) {Alist.add (nums[i]);d FS (list, n, K, depth + 1,nums,i); Alist.remove (alist.size ()-1);}}
Java for Leetcode 078 subsets