• [LeetCode#90]Subsets II


    Problem:

    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],
      []
    ]

    Analysis:

    At first glance, you would follow the same complex routine to solve permutation problem. 
    1. we need a indicator for position to check base case.
    2. we need a used array to indicate which words have already been used.
    3. we need a checking mechanism, so as to avoid palcing same value at the same position twice.
    
    Thus, we reach the following ugly solution.
    
        static public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> ret = new ArrayList<List<Integer>> ();
            if (nums == null || nums.length == 0)
                return ret;
            Arrays.sort(nums);
            ArrayList<Integer> path = new ArrayList<Integer> ();
            boolean[] used = new boolean[nums.length];
            helper(0, 0, nums, used, path, ret);
            return ret;
        }
        
        static public void helper(int pos, int index, int[] nums, boolean[] used, ArrayList<Integer> path, List<List<Integer>> ret)     {
            if (pos == nums.length) {
                ret.add(new ArrayList<Integer> (path));
                return;
            }
            for (int i = index; i < nums.length; i++) {
                if (i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue;
                if (!used[i]) {
                    used[i] = true;
                    helper(pos+1, i+1, nums, used, path, ret);
                    path.add(nums[i]);
                    helper(pos+1, i+1, nums, used, path, ret);
                    path.remove(path.size()-1);
                    used[i] = false;
                }
            }
        }
        
    To make things worse, it is not right for the following case.
    nums = [1, 1]
    output: [], [1], [1], [1, 1]
    expected: [], [1], [1, 1]
    The problem was caused by a ridiculous logic error:
            [1, 1]
        [1]
            [1]
    []
            [1]
        []
            []
    And this error is hard not solve, cause we have already solved the problem of placing two different elements with the same value at the same position.
    
    Actually, this problem could be sovled in a very very elegant way, without any need to record position, used array!!! The only thing we should keep in mind is that: we have no exact position concept now, a helper process is to place a element at a positon, but we do not know the eact position.
    
    Since the result must in no-descending order, we must sort the nums first. What's more, it is the tool of avoding duplicates!!!
    Arrays.sort(nums);
    
    1. Start from empty path: []
    2. For each search enter into the helper, we add it into result set. (That's the definition of subset. Right???)
    ret.add(new ArrayList<Integer> (path));
    3. Then we try to select a element from [start, nums.length - 1] to append it into the current subset.
    for (int i = start; i < nums.length; i++) {
        if (i > start && nums[i] == nums[i-1]) continue;
        path.add(nums[i]);
        helper(nums, i+1, path, ret);
        path.remove(path.size()-1);
    }
    Note 1: we are trying to place a element for the same position. we should avoid duplicates at here.
    The skill to avoid the duplicate is common. <only the first element share the value was placed>
    if (i > start && nums[i] == nums[i-1]) continue;
    
    Note 2: we do not need used array at here, cause for next placement, we always start from the element after the current placed element.
    path.add(nums[i]);
    helper(nums, i+1, path, ret);
    path.remove(path.size()-1);

    Solution:

    public class Solution {
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> ret = new ArrayList<List<Integer>> ();
            if (nums == null || nums.length == 0)
                return ret;
            ArrayList<Integer> path = new ArrayList<Integer> ();
            Arrays.sort(nums);
            helper(nums, 0, path, ret);
            return ret;
        }
        
        private void helper(int[] nums, int start, ArrayList<Integer> path, List<List<Integer>> ret) {
            ret.add(new ArrayList<Integer> (path));
            for (int i = start; i < nums.length; i++) {
                if (i > start && nums[i] == nums[i-1]) continue;
                path.add(nums[i]);
                helper(nums, i+1, path, ret);
                path.remove(path.size()-1);
            }
        }
    }
  • 相关阅读:
    PHP开发学习门户改版效果图投票
    怎样用js得到当前页面的url信息方法(JS获取当前网址信息)
    java 获取当期时间之前几小时的时间
    超人学院Hadoop大数据技术资源分享
    plsql导入一个目录下全部excel
    UML简单介绍
    Mod in math
    父母之爱子,则为之计深远是什么意思?_百度知道
    2014创客118新年大爬梯_活动行-国内最好的活动报名及售票平台!
    北京创客空间 BEIJING MAXPACE的小站
  • 原文地址:https://www.cnblogs.com/airwindow/p/4750014.html
Copyright © 2020-2023  润新知