代码随想录算法训练营第29天 | 491.递增子序列,46. 全排列,47. 全排列 II

代码随想录算法训练营第29天 | 491.递增子序列,46. 全排列,47. 全排列 II

491.递增子序列

  1. 子集问题 不需要终止条件 因为每个节点都要加入 所以每层递归最上面直接加入结果集就行了 或者加个判断条件
  2. 对于不能改变数组顺序的情况 就每次循环更新一次 map map不能定义在外面 这样上次循环的值还在Map中
  3. 对于递增的条件 每次加入元素前比较大小
  4. 假如不符合情况continue而不是break 因为后面可能还有符合情况的值
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        
        backtracing(nums,0);
        return result;

    }

    public void backtracing(int[] nums,int startIndex){
        //终止条件
        if(path.size()>1){
            result.add(new ArrayList<>(path));
        }
        //map 每一层for循环之后都是要清空的  
        Map<Integer,Integer> map = new HashMap<>();
        //单层递归逻辑
        for(int i=startIndex;i<nums.length;i++){
            //满足递增条件 和path最右值比较
            if(!path.isEmpty() && nums[i]<path.get(path.size()-1)){
                continue;
            }
            //树层去重 map记录出现次数
            if(map.getOrDefault(nums[i],0)>0){
                continue;
            }

            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
            path.add(nums[i]);
            backtracing(nums,i+1);
            path.remove(path.size()-1);
        }
        return;
    }
}

46. 全排列

  1. 此题需要遍历全部数组 因此不用startindex 而是需要一个记录元素是否用过
  2. 当递归到叶节点也就是 path长度与num一样结束
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used,false);
        backtracing(nums,used);
        return result;

    }
    //每一次循环都从头开始
    public void backtracing(int[] nums, boolean[] used){
        //终止条件  到叶节点收集数据
        if(path.size()==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }

        //单层递归逻辑
        for(int i =0;i<nums.length;i++){
            if(used[i]==true){
                continue;
            }
            used[i]=true;
            path.add(nums[i]);
            backtracing(nums,used);
            path.remove(path.size()-1);
            used[i] = false;

        }
        return;

    }
}

47. 全排列 II

  1. 此题树枝去重也可以得到答案 因为是排列问题 但是效率差 因为还要无用搜索
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used,false);
        Arrays.sort(nums);
        backtracing(nums,used);
        return result;
    }


    public void backtracing(int[] nums, boolean[] used){
        //终止条件
        if(path.size()==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }

        //单层递归逻辑
        for(int i=0;i<nums.length;i++){
            //树层去重
            if(i>0 && used[i-1]==false && nums[i]==nums[i-1]){
                continue;
            }

            //判断加入条件
            if(used[i]==false){
                used[i]=true;
                path.add(nums[i]);
                backtracing(nums,used);
                used[i]=false;
                path.remove(path.size()-1);
            }
            
        }
        return;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值