zoukankan      html  css  js  c++  java
  • [leetcode] Subsets II

    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.

    https://oj.leetcode.com/problems/subsets-ii/

    思路:关键是去重,先排序,然后根据后一个是否跟前一个相等来判断是否继续递归。

    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Solution {
        public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
    
            ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            // sort first
            Arrays.sort(num);
            sub(num, 0, tmp, ans);
            return ans;
        }
    
        public void sub(int[] num, int k, ArrayList<Integer> tmp, ArrayList<ArrayList<Integer>> ans) {
            ArrayList<Integer> arr = new ArrayList<Integer>(tmp);
            ans.add(arr);
    
            for (int i = k; i < num.length; i++) {
                // remove the dup
                if (i != k && num[i] == num[i - 1])
                    continue;
    
                tmp.add(num[i]);
                sub(num, i + 1, tmp, ans);
                tmp.remove(tmp.size() - 1);
            }
        }
    
        public static void main(String[] args) {
            System.out.println(new Solution().subsetsWithDup(new int[] { 1, 2, 2 }));
        }
    }
    View Code

    第二遍记录:

    只用增量法实现了去重。

    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Solution {
        public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
    
            ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            // sort first
            Arrays.sort(num);
            sub(num, 0, tmp, ans);
            return ans;
        }
    
        public void sub(int[] num, int k, ArrayList<Integer> tmp, ArrayList<ArrayList<Integer>> ans) {
            ArrayList<Integer> arr = new ArrayList<Integer>(tmp);
            ans.add(arr);
    
            for (int i = k; i < num.length; i++) {
    
                if (i == k || num[i] != num[i - 1]) {
                    tmp.add(num[i]);
                    sub(num, i + 1, tmp, ans);
                    tmp.remove(tmp.size() - 1);
                }
            }
        }
    
        public static void main(String[] args) {
            System.out.println(new Solution().subsetsWithDup(new int[] { 1, 2, 2 }));
        }
    }

    cc150上也看到了,第三遍走起。

      注意先排序。

      注意递归条件中的start,表明可以开始选取元素的索引,因为我们每次只允许从后面选,递归向下传递start值为 i+1(不是start+1,开始这里搞错了)。

    import java.util.ArrayList;
    
    public class Solution {
        public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
            ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
            if (num == null || num.length == 0)
                return res;
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            subsetsHelper(num, res, tmp, 0);
    
            return res;
        }
    
        private static void subsetsHelper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> tmp, int start) {
            res.add(new ArrayList<Integer>(tmp));
    
            for (int i = start; i < num.length; i++) {
    
                if (i == start || num[i] != num[i - 1]) {
                    tmp.add(num[i]);
                    subsetsHelper(num, res, tmp, i + 1);
                    tmp.remove(tmp.size() - 1);
                }
            }
    
        }
    
        public static void main(String[] args) {
            int[] a = { 1, 2, 2 };
    
            System.out.println(subsetsWithDup(a));
        }
    }

    参考:

    http://www.cnblogs.com/lautsie/p/3249869.html

  • 相关阅读:
    技术学习博2
    正则匹配两个特定词组中间的内容的写法
    springboot mitre HTTP Proxy Servlet POST hang timeout 不执行的解决方案
    JAVA实现的HTTP反向代理 [smiley-http-proxy-servlet]学习
    get方式url传递map的写法,control传入map数据url格式&params[key]=value&params[key2]=value2
    angular7 学习笔记记录--1. TypeScript基础
    Operator '>' cannot be applied to types 'boolean' and 'number'?
    记录在linux环境redis如何重启版本5.x
    ElasticSearch (with docker )入门学习
    Dataway【实测】,让服务飞起来,让SpringBoot不再需要Controller、Service、DAO、Mapper
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3819323.html
Copyright © 2011-2022 走看看