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 }
  • 相关阅读:
    完全N叉树寻找祖先
    MySql_Front新建数据库遇到访问地址冲突问题
    C++金额的中文大写
    STL_sort cmp
    螺旋数组
    ~
    Struts向JSP中传值
    Struts1-配置文件部分
    jQuery Ajax 的 load()方式
    jquery animate
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6509397.html
Copyright © 2011-2022 走看看