Subsets II

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

Tips:

和subsets一样,注意for里面i从depth开始,然后再进recursion是i + 1不是depth + 1;

第一个是不加hash的,第二个是加了hash的,第一个方法是大神插入法,第四个是位运算。

Code:

方法1:

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        //HashSet<List<Integer>> set = new HashSet<>();
        Arrays.sort(nums);
        dfs(nums, result, new ArrayList<Integer>(), 0);
        return result;
    }
    private void dfs(int[] nums, List<List<Integer>> result,  ArrayList<Integer> path, int depth) {
            result.add(new ArrayList<Integer>(path));
        for (int i = depth; i < nums.length; i++) {
            if (i > depth && nums[i] == nums[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            dfs(nums, result, path, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

方法2:

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        HashSet<List<Integer>> set = new HashSet<>();
        Arrays.sort(nums);
        dfs(nums, result, set, new ArrayList<Integer>(), 0);
        return result;
    }
    private void dfs(int[] nums, List<List<Integer>> result, HashSet<List<Integer>> set, ArrayList<Integer> path, int depth) {
        if (!set.contains(path)) {
            set.add(new ArrayList<Integer>(path));
            result.add(new ArrayList<Integer>(path));
        }
        for (int i = depth; i < nums.length; i++) {
            path.add(nums[i]);
            dfs(nums, result, set, path, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

方法3:

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        result.add(new ArrayList<Integer>());
        HashSet<List<Integer>> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            List<List<Integer>> newRes = new ArrayList<>(result); 
            for (List<Integer> l : result) {
                ArrayList<Integer> newList = new ArrayList<>(l);
                newList.add(nums[i]);
                if (set.contains(newList)) continue;
                set.add(newList);
                newRes.add(newList);
            }
            result = newRes;
        }
        return result;
    }
}

方法4:

class Solution {
    public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> S) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        HashSet<ArrayList<Integer>> set = new HashSet<>();
        Collections.sort(S);
        if (S== null || S.size() == 0) {
            return result;
        }
        result.add(new ArrayList<Integer>());
        int n = S.size();
        for (int i = 1; i < (1 << n); i++) {
            ArrayList<Integer> sub = new ArrayList<Integer>();
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) != 0) {
                    sub.add(S.get(j));
                }
            }
            if (!set.contains(sub)) {
                set.add(sub);
                result.add(sub);
            }
        }
        return result;
    }
}

results matching ""

    No results matching ""