zoukankan      html  css  js  c++  java
  • Subset II leetcode java

    题目

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

    Note:

    • Elements in a subset must be in non-descending order.
    • The solution set must not contain duplicate subsets.

    For example,
    If S = [1,2,2], a solution is:

    [
      [2],
      [1],
      [1,2,2],
      [2,2],
      [1,2],
      []
    ]
    

    题解
    这个在subset题的第一种解法的基础上有两种解决办法。。
    1. 在添加res时候判断是否res中已经存过该item了。没存过的才存保证子集唯一性。
    代码如下:
     1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
     2         if(item.size()==len){
     3             if(!res.contains(item))
     4                 res.add(new ArrayList<Integer>(item));
     5             return;
     6         }
     7         for(int i=start; i<S.length;i++){
     8             item.add(S[i]);
     9             dfs(S, i+1, len, item, res);
    10             item.remove(item.size()-1);
    11         }
    12 
    13     }
    14     
    15     public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
    16         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
    17         ArrayList<Integer> item = new ArrayList<Integer>();
    18         if(S.length==0||S==null)
    19             return res;
    20         
    21         Arrays.sort(S);
    22         for(int len = 1; len<= S.length; len++)
    23             dfs(S,0,len,item,res);
    24             
    25         res.add(new ArrayList<Integer>());
    26         
    27         return res;
    28     }

    2. 还有一种方法就是在DFS过程中 当有重复元素出现就只对当前这个元素走一起,其他重复元素跳过。参考:http://blog.csdn.net/worldwindjp/article/details/23300545

    代码如下:
     1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
     2         if(item.size()==len){
     3             res.add(new ArrayList<Integer>(item));
     4             return;
     5         }
     6         for(int i=start; i<S.length;i++){
     7             item.add(S[i]);
     8             dfs(S, i+1, len, item, res);
     9             item.remove(item.size()-1);
    10             while(i<S.length-1&&S[i]==S[i+1])//跳过重复元素
    11                 i++;
    12         }
    13 
    14     }
    15     
    16     public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
    17         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
    18         ArrayList<Integer> item = new ArrayList<Integer>();
    19         if(S.length==0||S==null)
    20             return res;
    21         
    22         Arrays.sort(S);
    23         for(int len = 1; len<= S.length; len++)
    24             dfs(S,0,len,item,res);
    25             
    26         res.add(new ArrayList<Integer>());
    27         
    28         return res;
    29     }       

  • 相关阅读:
    图像旋转与图像缩放及Matlab代码实现
    主成分分析 matlab手把手教操作、SPSS、python实例分析
    直方图均衡化与Matlab代码实现
    kNN算法基本原理与Python代码实践
    MATLAB 均方根误差MSE、两图像的信噪比SNR、峰值信噪比PSNR、结构相似性SSIM
    vue实战——对没见过的东西的科普以及第一次踩坑
    记录关于js模块的浅薄探索(一)——从别人博客中的理解
    js中的同步异步
    webpack踩坑记录(一)
    vue入门(二) ——监听属性,样式绑定
  • 原文地址:https://www.cnblogs.com/springfor/p/3879853.html
Copyright © 2011-2022 走看看