zoukankan      html  css  js  c++  java
  • 90. 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],
      []
    ]

    此题以前做过类似的题目,不做解释,代码如下:
     1 public class Solution {
     2     public List<List<Integer>> subsetsWithDup(int[] nums) {
     3         List<List<Integer>> res= new ArrayList<>();
     4         Arrays.sort(nums);
     5         helper(res,new ArrayList<Integer>(),nums,0);
     6         return res;
     7     }
     8     public void helper(List<List<Integer>> res,List<Integer> list,int[] nums,int index){
     9         if(!res.contains(list)){
    10             res.add(new ArrayList<Integer>(list));
    11         }
    12         for(int i=index;i<nums.length;i++){
    13             list.add(nums[i]);
    14             helper(res,list,nums,i+1);
    15             list.remove(list.size()-1);
    16         }
    17         return;
    18         
    19     }
    20 }
  • 相关阅读:
    元素定位方法与等待
    xpath定位的总结
    模拟登录
    shell的数组
    shell的函数
    shell的循环
    shell的流程控制语句case
    shell的while循环
    shell的if条件判断
    shell的for循环
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6509397.html
Copyright © 2011-2022 走看看