90. Subsets II

来源:互联网 发布:阿里云dns解析 a记录 编辑:程序博客网 时间:2024/05/29 08:15

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: 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],  []]
求子集,去掉重复的,程序如下所示:

class Solution {    public void traceBack(int[] nums, List<List<Integer>> llst, List<Integer> lst, int begin, int len){        if (begin > len){            return;        }        llst.add(new ArrayList(lst));        for (int i = begin; i < len; ++ i){            if (i != begin&&nums[i] == nums[i-1]){                continue;            }            lst.add(nums[i]);            traceBack(nums, llst, lst, i + 1, len);            lst.remove(lst.size() - 1);        }    }    public List<List<Integer>> subsetsWithDup(int[] nums) {        List<Integer> lst = new ArrayList<>();        List<List<Integer>> llst = new ArrayList<>();        Arrays.sort(nums);        traceBack(nums, llst, lst, 0, nums.length);        return llst;    }}




原创粉丝点击