90. Subsets II

来源:互联网 发布:云计算ppt模板百度云 编辑:程序博客网 时间:2024/06/11 10:04

90. Subsets II

  • 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 List<List<Integer>> subsetsWithDup(int[] nums) {     Arrays.sort(nums);      List<List<Integer>> res = new ArrayList<>();      backTracking(res, new ArrayList<>(), 0, nums);      return res;  }  private void backTracking(List<List<Integer>> res, List<Integer> listItem, int start,int []nums) {      res.add(new ArrayList<>(listItem));      for(int i=start;i<nums.length;i++) {          if (i>start && nums[i] == nums[i - 1]) {              continue;          }          listItem.add(nums[i]);          backTracking(res, listItem, i + 1, nums);          listItem.remove(listItem.size() - 1);      }  }}