-
Notifications
You must be signed in to change notification settings - Fork 0
90. Subsets II
Linjie Pan edited this page Jun 9, 2019
·
1 revision
public void traverse(List<List<Integer>> resultList, List<Integer> currentList, int index, int[] nums) {
resultList.add(new ArrayList<Integer>(currentList)); // 3. add currentList to final result list
for(int i = index; i < nums.length; i++) {
if( i > index && nums[i] == nums[i - 1] ) // 2. duplicate removal
continue;
currentList.add(nums[i]);
traverse(resultList, currentList, i + 1, nums);
currentList.remove(currentList.size() - 1);
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums); // 1. sort the array
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
traverse(resultList, new ArrayList<Integer>(), 0, nums);
return resultList;
}