Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 解题思路: 这道题是要找数组的所有子集。那想法跟上道题一样。区别就是在每次建一个新的list的时候,都将其存到List<List<Integer>>里面就行。public class Solution { public List<List<Integer>> result = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] nums) { List<Integer> com = new ArrayList<Integer>(); result.add(com); addsub(0,nums,com); return result; } public void addsub(int i,int[] nums,List<Integer> com) { int length = nums.length; if(i < length) { for(int j = i;j<length;j++) { List<Integer> newcom = new ArrayList<Integer>(); newcom.addAll(com); newcom.add(nums[j]); result.add(newcom); addsub(j+1,nums,newcom); } } } }