package Backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SubsetsII{
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        ArrayList<List<Integer>> ret = new ArrayList();
        ret.add(new ArrayList<>());
        Arrays.sort(nums);

        int end = 0;// end idx of last adding
        int be = 0;
        for(int i=0; i<nums.length; i++){
            be = i>0 && nums[i]==nums[i-1] ? end : 0;
            end = ret.size();
            for(int j=be; j<end; j++){
                ArrayList newSubset = (ArrayList)(((ArrayList)ret.get(j)).clone());
                newSubset.add(nums[i]);
                ret.add(newSubset);
            }     
        }

        return ret;
    }
}
@@ -177,16 +177,53 @@
 nums) %7B%0A
+        // %E5%88%86%E6%9E%90%E9%A2%98%E7%9B%AE%EF%BC%8C%E6%8A%93%E4%BD%8F%E7%89%B9%E6%80%A7%EF%BC%8C%E5%AD%90%E9%9B%86%E4%BA%A7%E7%94%9F%E5%AD%90%E9%9B%86%0A        %0A
         
@@ -782,8 +782,9 @@
 %0A    %7D%0A%7D
+%0A

Update SubsetsII.java