90 Subsets ii

public class Solution {
    public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] nums) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        if (nums == null || nums.length == 0) {
            return res;
        }
        Arrays.sort(nums);
        dfs(nums, 0, res, list);
        return res;
    }
    public void dfs(int[] nums, int pos, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> list) {
        res.add(new ArrayList<Integer>(list));
        for (int i = pos; i < nums.length; i++) {
            if (i != pos && nums[i] == nums[i - 1]) {
                continue;
            }
            list.add(nums[i]);
            dfs(nums, i + 1, res, list);
            list.remove(list.size() - 1);
        }
    }
}

 

posted on 2015-05-28 10:23  kikiUr  阅读(92)  评论(0编辑  收藏  举报