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], [] ]
public class Solution { public List<List<Integer>> subsets(int[] nums) { if(nums==null){ return null; } List<List<Integer>> resList=new ArrayList<List<Integer>>(); List<Integer> item=new ArrayList<Integer>(); Arrays.sort(nums); backTracking(nums, 0, item, resList); resList.add(new ArrayList<Integer>()); return resList; } public void backTracking(int[] nums, int start, List<Integer> item, List<List<Integer>> resList){ for(int i=start; i<nums.length; i++){ item.add(nums[i]); resList.add(new ArrayList<Integer>(item)); backTracking(nums, i+1, item, resList); item.remove(item.size()-1); } } }
原文:http://www.cnblogs.com/incrediblechangshuo/p/5925380.html