回溯-子集问题

一些子集问题要记录树上的所有节点,因此终止条件可加可不加

1. 子集

在这里插入图片描述
子集

该题和排列问题基本相似,本题需要收集所有的结果。

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>(); 
    public List<List<Integer>> subsets(int[] nums) {

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

        subsetsHelper(nums,0);
        return result;
    }

    void subsetsHelper(int[] nums,int startIndex){
        //要添加每一组元素
        result.add(new ArrayList<>(path));

        for(int i = startIndex;i<nums.length;i++){
            path.add(nums[i]);
            subsetsHelper(nums,i+1);
            path.removeLast();
        }
    }
}

2.子集 II

在这里插入图片描述
子集 II

去重,建立一个boolean数组。

class Solution {
    List<List<Integer>> res = new ArrayList<>();//存放答案
    LinkedList<Integer> path = new LinkedList<>();//用来存放符合条件的结果
    boolean[] used;//用来去重的数组
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if(nums.length==0){
            res.add(new ArrayList<>(path));
            return res;
        }
        Arrays.sort(nums);
         used = new boolean[nums.length];
        subsetsWithDupHelper(nums,0);
        return res;
    }
    //startIndex表示嵌套了几个for循环
    void subsetsWithDupHelper(int[] nums,int startIndex){
        //终止条件
        res.add(new ArrayList<>(path));
        

        for(int i = startIndex;i<nums.length;i++){
            //去重,去重同枝,不去重同层
            if(i>0&&nums[i]==nums[i-1]&&!used[i-1]){
                continue;
            }
            path.add(nums[i]);
            used[i]=true;
            subsetsWithDupHelper(nums,i+1);
            //回溯
            path.removeLast();
            used[i]=false;
        }
    }
}

3.递增子序列

在这里插入图片描述
递增子序列

去重

class Solution {

    List<List<Integer>> res = new ArrayList<>();//输出答案
    LinkedList<Integer> path = new LinkedList<>();//记录路径

    public List<List<Integer>> findSubsequences(int[] nums) {
        backTracking(nums,0);
        return res;
    }

    void backTracking(int[] nums,int startIndex){
        //确定终止条件
        
        if(path.size()>1){
            res.add(new ArrayList<>(path));
            //return;
        }
        //单层逻辑
        int[] used = new int[201];
        for(int i=startIndex;i<nums.length;i++){
            //去重
            if(!path.isEmpty()&&nums[i]<path.get(path.size()-1)||(used[nums[i]+100]==1)){
                continue;
            }
            used[nums[i]+100]=1;
            path.add(nums[i]);
            //回溯
            backTracking(nums,i+1);
            path.removeLast();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值