zoukankan      html  css  js  c++  java
  • leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution is:

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

    思路:这一题比subsets多一了一道反复,详细代码例如以下:

    public class Solution {
        boolean[] b;
        Set<String> set;
        List<List<Integer>> list;
        Set<String> set1;
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            b = new boolean[nums.length];
            set = new HashSet<String>();
            list = new ArrayList<List<Integer>>();
            set1 = new HashSet<String>();
            
            Arrays.sort(nums);
            count(nums,"",nums.length,0);
            return list;
        }
        
        private void count(int[] nums,String s,int n,int j){
                //没有反复才加入
                if(set.add(s)){
                   //以","切割数组
                   String[] sa = s.split(",");
                   List<Integer> al = new ArrayList<Integer>();
                   for(int i = 0; i < sa.length; i++){
                	   if(sa[i].length() > 0){
                		   al.add(Integer.parseInt(sa[i]));
                	   }
                   }
                   Collections.sort(al);
                   if(set1.add(al.toString()))
                	   list.add(al);
                }
            
            for(int i = j; i < nums.length;i++){
                if(!b[i]){
                    b[i] = true;
                    count(nums,s + "," + nums[i],n-1,i+1);
                    b[i] = false;
                }
            }
        }
        
    }


    以下这样的写法更简洁:

    public class Solution {
        List<List<Integer>> list;//结果集
        List<Integer> al;//每一项
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            list = new ArrayList<List<Integer>>();
            al = new ArrayList<Integer>();
            Arrays.sort(nums);
            //排列组合
            count(nums,al,0);
            return list;
        }
        
        private void count(int[] nums,List<Integer> al,int j){
            
        	list.add(new ArrayList<Integer>(al));//不反复的才加入
            
            for(int i = j; i < nums.length;i++){
            	if(i == j || nums[i] != nums[i-1]){//去除反复
            		al.add(nums[i]);//加入
                    count(nums,al,i+1);
                    al.remove(al.size()-1);//去除。为下一个结果做准备
            	}
            }
        }
        
    }


  • 相关阅读:
    devise 异步发邮件
    ubuntutweak for lucid
    960gs blueprint
    Amoeba for mysql 0.31发布(读写分离、负载均衡、Failover、数据切分)
    Google App Servlet容器转型 – 从Tomcat到Jetty
    DBeaver
    用simple from暂不用formtastic
    [SQL Server]ORDER BY的问题
    PHP pathinfo() 函数
    php中使用head进行二进制流输出,让用户下载PDF等附件的方法
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7230404.html
Copyright © 2011-2022 走看看