代码随想录day24 Java版

491.递增子序列

HashSet 用于记录已经选择过的元素,确保同一层级中不会选择重复的元素。这样可以保证在回溯的过程中,只会选择不同的元素组合,避免产生重复的递增子序列。

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        backtrack(nums,0);
        return res;
    }
    void backtrack(int[] nums, int start){
        if(path.size() >= 2) res.add(new ArrayList<>(path));            
        HashSet<Integer> hs = new HashSet<>();
        for(int i = start; i < nums.length; i++){
            if(!path.isEmpty() && path.get(path.size() -1) > nums[i] || hs.contains(nums[i])) continue;
            hs.add(nums[i]);
            path.add(nums[i]);
            backtrack(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

46.全排列

梦开始的地方,最开始写回溯不会用标记数组,递归只是处理循环层数不确定的问题

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {  
        backtrack(nums,path,0);
        return res;
    }
    void backtrack(int[] nums,List<Integer> path,int start){
        if(path.size()==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=start;i<nums.length;i++){
            if(!path.contains(nums[i])){
                path.add(nums[i]);
                backtrack(nums, path,0);
                path.removeLast();
            }     
        }
    }
}

加上标记数组就是剪枝了·

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) return res;
        used = new boolean[nums.length];
        backtrack(nums);
        return res;
    }
    void backtrack(int[] nums){
        if (path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++){
            if (used[i]) continue;
            used[i] = true;
            path.add(nums[i]);
            backtrack(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}

47.全排列 II

跟上一题相比,在每次循环中,如果当前元素与前一个元素相同且前一个元素未被使用过,则跳过当前元素,以避免生成重复的排列。

class Solution {
    List<List<Integer>> res = new LinkedList<>();
    List<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        used = new boolean[nums.length];
        Arrays.sort(nums);
        backtrack(nums, 0);
        return res;
    }
    void backtrack(int[] nums,int start){
        if(nums.length==path.size()) res.add(new LinkedList<>(path));
        for(int i=0;i<nums.length;i++){
            if(i>0&&nums[i]==nums[i-1]&&!used[i-1]) continue;
            if(!used[i]){
                path.add(nums[i]);
                used[i] = true;
                backtrack(nums, i+1);
                path.removeLast();
                used[i] = false;
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值