回溯算法part4

回溯算法part4

491. 非递减子序列 - 力扣(LeetCode)

如图所示,两个注意点

  • 同一父节点下本层不可重复使用(for循环中)
  • path中所取元素不能小于最后一个元素

所以之前的去重逻辑是

if(i > startIndex && nums[i] == nums[i - 1]){break;}

现在则不同,本题中nums无序,只能使用哈希来判断是否重复使用

代码如下:

class Solution {

    List<List<Integer>> res = new ArrayList<>();

    List<Integer> path = new ArrayList<>();

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

    public void backtracking(int[] nums, int startIndex) {
        if(path.size() > 1){
            res.add(new ArrayList<>(path));
            //不要return,因为要遍历整颗树
        }
        HashSet<Integer> icon = new HashSet<>();
        for(int i = startIndex; i < nums.length; i++){
            if(icon.contains(nums[i]) || (!path.isEmpty() && nums[i] < path.get(path.size() - 1))){
                continue;
            }
            icon.add(nums[i]);
            path.add(nums[i]);
            backtracking(nums, i + 1);
            path.removeLast();
        }
    }
}

46. 全排列 - 力扣(LeetCode)

用HashSet栈溢出了,改用used数组通过

回溯算法中的排列问题:不用startIndex,因为要遍历整个树

代码如下:

class Solution {

    List<List<Integer>> res = new ArrayList<>();

    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] used = new boolean[nums.length];
        backtracking(nums, used);
        return res;
    }

    public void backtracking(int[] nums, boolean[] used) {
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
        }

        for(int i = 0; i < nums.length; i++){
            if(used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            backtracking(nums, used);
            used[i] = false;
            path.removeLast();
        }
    }
}

47. 全排列 II - 力扣(LeetCode)

这道题真难住我了

关于这个used数组是干什么的???

ok,去看了之前的课程,发现是学的时候根本没看到used这一部分

used既管着同一树层,又管着同一树枝

关于下面的代码:

if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
    continue;
}

当前面的条件满足时(i > 0 && nums[i] == nums[i - 1])

used[i - 1] == true代表同一树枝使用过

used[i - 1] == false代表同一树层使用过

class Solution {

    List<List<Integer>> res = new ArrayList<>();

    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        boolean[] used = new boolean[nums.length];
        backtracking(nums, used);
        return res;
    }

    public void backtracking(int[] nums, boolean[] used) {
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
        }

        for(int i = 0; i < nums.length; i++){
            if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理
            if(used[i] == false){
                used[i] = true;
                path.add(nums[i]);
                backtracking(nums, used);
                used[i] = false;
                path.removeLast();
            }
        }
    }
}

看了好几遍表示还是很懵

posted @ 2025-06-18 22:25  泡芙猪  阅读(13)  评论(0)    收藏  举报