1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import static org.junit.Assert.assertSame;
public class Subsets { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (nums == null || nums.length == 0) { return result; } Arrays.sort(nums); helper(0, result, new ArrayList<Integer>(), nums); return result; } private void helper(int start, List<List<Integer>> result, List<Integer> crt, int[] nums) { if (start >= nums.length) { result.add(crt); return; } helper(start + 1, result, new ArrayList<Integer>(crt), nums); crt.add(nums[start]); helper(start + 1, result, new ArrayList<Integer>(crt), nums); } @Test public void test() { int[] nums = new int[]{1, 2, 3}; List<List<Integer>> result = subsets(nums); assertSame(8, result.size()); } }
|