`

Leetcode - Subset II

 
阅读更多
[分析]  延续Subset三种思路,关键是添加去重处理
思路1:仅需在递归函数循环前面的加个if判断,这个技巧在Combination,Permutation中均使用。这个去重处理是三种实现中最简洁的,不容易出错。
思路2和思路3参考Code Ganker博客,分别是递归和迭代的思路,去重处理花了番功夫理解。
[ref]
subset II: http://blog.csdn.net/linhuanmars/article/details/24613193

public class Solution {
    // Method 1
    public List<List<Integer>> subsetsWithDup1(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        }
        Arrays.sort(nums);
        recur(nums, 0, new ArrayList<Integer>(nums.length), result);
        return result;
    }
    public void recur(int[] nums, int start, List<Integer> subset, List<List<Integer>> result) {
        result.add(new ArrayList<Integer>(subset));
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1])
                continue;
            subset.add(nums[i]);
            recur(nums, i + 1, subset, result);
            subset.remove(subset.size() - 1);
        }
    }
    
    // Method 2
    public List<List<Integer>> subsetsWithDup2(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        }
        Arrays.sort(nums);
        ArrayList<Integer> lastSize = new ArrayList<Integer>();
        lastSize.add(0);
        return recur(nums, nums.length - 1, lastSize);
    }
    public List<List<Integer>> recur(int[] nums, int idx, ArrayList<Integer> lastSize) {
        if (idx < 0) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            result.add(new ArrayList<Integer>());
            return result;
        }
        List<List<Integer>> result = recur(nums, idx - 1, lastSize);
        int size = result.size();
        int start = 0;
        if (idx > 0 && nums[idx] == nums[idx - 1])
            start = lastSize.get(lastSize.size() - 1);
        for (int i = start; i < size; i++) {
            List<Integer> newSubset = new ArrayList<Integer>(result.get(i));
            newSubset.add(nums[idx]);
            result.add(newSubset);
        }
        lastSize.add(size);
        return result;
    }
    
    // Method 3
    public List<List<Integer>> subsetsWithDup3(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(new ArrayList<Integer>());
        if (nums == null || nums.length == 0) {
            return result;
        }
        Arrays.sort(nums);
        int start = 0;
        int lastSize = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1])
                start = lastSize;
            else
                start = 0;
            lastSize = result.size();
            for (int j = start; j < lastSize; j++) {
                List<Integer> newSubset = new ArrayList<Integer>(result.get(j));
                newSubset.add(nums[i]);
                result.add(newSubset);
            }
        }
        return result;
    }
    
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics