Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
Analysis
Almost same question with  Combination Sum II
Use DFS
Code
public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        if (nums.length==0) {return result;}
        Arrays.sort(nums);
        helper(nums, 0, list, result);
        return result;
    }
    private void helper(int[] nums, int pos, List<Integer> list, List<List<Integer>> result){
        //do not need conditon to add list, add whenever this function is called
        result.add(new ArrayList<Integer>(list));
        for (int i = pos; i < nums.length; i++){
            //delete repeat conditions
            if (i!= pos && nums[i]== nums[i-1]) {continue;}
            list.add(nums[i]);
            //Pay attention here is i+1, not pos+1
            helper(nums, i+1, list, result);
            list.remove(list.size()-1);
        }
    }
}
Tips
- sort array: Arrays.sort(arr)
 - sort list: Collections.sort(list)
 
Question
Delete repeat part sill hard to understand
To do: 九章算法第七章,subarray问题