代码随想录打卡第29天|491.递增子序列;46.全排列;47.全排列 II

491.递增子序列

关键点1:将path加入res,结果是除开size<2的所有节点(path.size() > 1 );

关键点2:终止条件可要可不要,因为下面的循环也会终止;

关键点3:continue的几个条件;

3-1:nums[i] < path最后一个值,不加;
3-2:set 里曾经有过这个值了,不加,实现了树层去重;

关键点4:set.add(nums[i]);加入,递归,弹出,set不用再弹出,是因为set每层会新建新的set。

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        backStracking(nums,0);
        return res;

    }
    public void backStracking(int[] nums,int starIndex){
        if(path.size() > 1){
            res.add(new ArrayList<>(path));
        }
        // 终止条件
        if(starIndex >= nums.length){
            return;
        }
        // 单层循环条件
        HashSet<Integer> set = new HashSet<>();
        for(int i = starIndex;i < nums.length;i++){
            // nums[i] < path最后一个值,不加;
            // set里曾经有过这个值了,不加,实现了树层去重;
            if(!path.isEmpty() && nums[i] < path.get(path.size()-1) ||set.contains(nums[i])){
                continue;
            }
            set.add(nums[i]);
            path.add(nums[i]);
            backStracking(nums,i+1);
            path.remove(path.size()-1);
        }

    }
}

46.全排列

关键点1:当path的长度等于这个原始数组的长度则证明到了根节点;

关键点2:单层循环条件可以从0开始,因为排列问题,1,2和2,1是两个排列,之前使用过的元素不再使用,用used来排除;

关键点3:used[i] = 1,添加,递归,used[i] = 0,弹出。

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        int[] used = new int[nums.length];
        backStacking(nums,used);
        return res;
    }
    public void backStacking(int[] nums,int[] used){
        // 结束条件
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }

        // 单层循环条件
        for(int i = 0; i < nums.length;i++){
            // 使用过的元素不再使用
            if(used[i] == 1){
                continue;
            }
            used[i] = 1;
            path.add(nums[i]);
            backStacking(nums,used);
            used[i] = 0;
            path.remove(path.size() - 1);
            
        }

    }
}

47.全排列 II

关键点1:当path的长度等于这个原始数组的长度则证明到了根节点;

关键点2:单层循环条件可以从0开始,因为排列问题,1,2和2,1是两个排列,之前使用过的元素不再使用,用used来排除;

关键点3:与全排列问题的区别在于,它有重复元素,需要用树层去重,用 i>0 && nums[i] == nums[i - 1] && used[i-1] == 0 进行树层去重

关键点3:used[i] = 1,添加,递归,used[i] = 0,弹出。

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        int[] used = new int[nums.length];
        backStracking(nums,used);
        return res;
    }
    public void backStracking(int[] nums,int[] used){
        // 结束条件
        if(path.size()==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }

        // 单层循环条件
        for(int i = 0;i < nums.length;i++){
            // 树层去重
            if(i>0 && nums[i] == nums[i - 1] && used[i-1] == 0){
                continue;
            }
            // 使用过的元素不再使用
            if(used[i] == 1){
                continue;
            }
            used[i] = 1;
            path.add(nums[i]);
            backStracking(nums,used);
            path.remove(path.size() - 1);
            used[i] = 0;
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值