90. Subsets II

来源:互联网 发布:win7下制作mac启动u盘 编辑:程序博客网 时间:2024/05/22 13:01

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

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],  []]

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



原创粉丝点击