LeetCode DFS 78 Subsets 90 Subsets ll

别说谁变了你拦得住时间么 提交于 2019-12-21 14:41:26
  1. Subsets
    Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

DFS (backtracking)

详情可以参考九章算法基础 lecture 1

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        //ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if (nums == null) {
            return result; 
        }
        
        if (nums.length == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        }
        
        Arrays.sort(nums);
        // find all the subsets that start with [], put these into result
        helper(nums, 0, new ArrayList<Integer>(), result);
        
        return result;
    }
    
    // find all the subsets that 
    // start with 'subset' and end with a number after nums[offset]
    // put these into result
    public void helper (int[] nums,
                        int offset,
                        ArrayList<Integer> subset,
                        List<List<Integer>> result) {
        // the reason why we create a new ArrayList with same value as subset is that
        // we don't want any upcoming operation affect the value stored in result
        result.add(new ArrayList<Integer>(subset));
        for (int i = offset; i < nums.length; i++ ) {
            subset.add(nums[i]);
            // find all the subsets that start with 'subset', put these into result
            helper(nums, i + 1, subset, result);
            subset.remove(subset.size() - 1);
            // backtracking
            
        }
        
    }
}
// 递归三要素
// 1 递归的定义:在nums中寻找所有以subset开头的子集,并放入results
// 2 递归的拆解:
// result里面add new ArrayList<Integer>(subset)是为了即使后面操作改变了subset的值,也不会影响result里面的值,因为new ArrayList<Integer>(subset)就相当于克隆了一份subset放到result里面
// 3 递归的出口

90. Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

solution

考虑duplicates时只需要从第一个数开始添加,不可以从中间开始,就可以了
和78题的代码相比只有helper里面for循环添加了一个if语句

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            //ArrayList<ArrayList<Integer>> result = new ArrayList<>();
            if (nums == null) {
                return result;
            }

            if (nums.length == 0) {
                result.add(new ArrayList<Integer>());
                return result;
            }

            Arrays.sort(nums);
            // find all the subsets that start with [], put these into result
            helper(nums, 0, new ArrayList<Integer>(), result);

            return result;
    }
    
            public void helper (int[] nums,
                            int offset,
                            ArrayList<Integer> subset,
                            List<List<Integer>> result) {
            // the reason why we create a new ArrayList with same value as subset is that
            // we don't want any upcoming operation affect the value stored in result
            result.add(new ArrayList<Integer>(subset));
            for (int i = offset; i < nums.length; i++ ) {
                // deal with duplicates cases like [1,2,2,3]
                if (i != offset && nums[i] == nums[i-1]) {
                    continue;
                }
                subset.add(nums[i]);
                // find all the subsets that start with 'subset', put these into result
                helper(nums, i + 1, subset, result);
                // backtracking
                subset.remove(subset.size() - 1);
            }

        }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!