LeetCode DFS 78 Subsets 90 Subsets ll

  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);
            }

        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值