1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Solution { List<List<Integer>> result = new ArrayList<List<Integer>>(); public List<List<Integer>> subsetsWithDup(int[] nums) { if (nums == null || nums.length == 0) return result; Arrays.sort(nums); rec(new ArrayList<Integer>(), 0, nums, true); return result; } private void rec(List<Integer> fromUpper, int index, int[]nums, boolean prevUsed){ if (index == nums.length){ result.add(new ArrayList<Integer>(fromUpper)); return; } if (index != 0 && nums[index] == nums[index-1] &&!prevUsed){ rec(fromUpper, index+1, nums, false); return; } fromUpper.add(nums[index]); rec(fromUpper, index+1, nums, true); fromUpper.remove(fromUpper.size()-1); rec(fromUpper, index+1, nums, false); } } |
Sunday, November 22, 2015
[leetcode]subset ii
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment