DFS常见算法

DFS

78. 子集

难度中等1101

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同

方法一:迭代

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        int n = nums.length;
        // 枚举0~2^n-1,也就是子集的个数
        List<List<Integer>> res = new ArrayList<>();
        for(int i = 0; i < (1 << n); i++){
            List<Integer> t = new ArrayList<>();
            // 通过求出i的二进制位上的1来表示是否去对应j位置上的数字
            for(int j = 0; j < n; j++){
                if((i >> j & 1) == 1){
                    t.add(nums[j]);
                }
            }
            res.add(t);
        }
        return res;
    }
}

方法二:回溯

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        //dfs,对于每个位置有两种可能性,一个是添加进子集,不加入子集
        int n = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();
        dfs(res,path,n,0,nums);
        return res;
    }
    private void dfs(List<List<Integer>> res, Deque<Integer> path, int n,int pos,int[] nums){
        if(pos == n){
            res.add(new ArrayList(path));
            return;
        }
        path.addLast(nums[pos]);
        dfs(res,path,n,pos+1,nums);
        path.removeLast();
        dfs(res,path,n,pos+1,nums);
    }
}
90. 子集 II

难度中等523

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

方法一:回溯

class Solution {
    List<List<Integer>> res = null;
    boolean[] st;
    Deque<Integer> path = null;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        res = new ArrayList<>();
        path = new LinkedList<>();
        st = new boolean[n];
        dfs(nums,n,0);
        return res;
    }
    private void dfs(int[] nums, int n, int idx){
        if(idx == n){
            res.add(new ArrayList<>(path));
            return;
        }
        dfs(nums,n,idx+1);
        if(idx > 0 && !st[idx-1] && nums[idx] == nums[idx-1]){
            return;
        }
        st[idx] = true;
        path.addLast(nums[idx]);
        dfs(nums,n,idx+1);
        st[idx] = false;
        path.removeLast();
    }
}

方法二:迭代

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for(int i = 0; i < (1 << n); i++){
            List<Integer> t = new ArrayList<>();
            boolean flag = false;
            for(int j = 0; j < n; j++){
                if((i >> j & 1) == 1){
                    // 当前值x,如果在x前面存在与x相同的y,并且没有取到,
                    // 那么取x的时候结果就包含在y的子集中
                    // 这是为什么呢,例如 nums[1,2,2],第3个子集为取0,1位置上的数,也就是{1,2}
                    // 然后到了第5个子集,这个时候取0,2位置上的数,也是{1,2};
                    // 可以看到第3个子集已经包合了第5个子集
                    if(j > 0 && (i >> j-1 & 1) == 0 && nums[j] == nums[j-1]){
                        flag = true;
                        break;
                    }
                    t.add(nums[j]);
                }
            }
            if(!flag){
                res.add(t);
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值