代码随想录Day29

Day 29 回溯算法part05

今日任务

  • 491.递增子序列
  • 46.全排列
  • 47.全排列 II

代码实现

491.递增子序列

 public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(nums, 0, path, result);
        return result;
    }

    void backtracking(int[] nums, int startIndex, List<Integer> path, List<List<Integer>> result) {
        if (path.size() > 1) {
            result.add(new ArrayList<>(path));
        }
        HashSet<Integer> set = new HashSet<>();

        for (int i = startIndex; i < nums.length; i++) {
            if ((path.size() > 0 && nums[i] < path.get(path.size() - 1)) || set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            path.add(nums[i]);
            backtracking(nums, i + 1, path, result);
            path.remove(path.size() - 1);
        }
    }

46.全排列
这里我没有用标记已使用的方法,而是用path.contains判断,结果到下一道题就行不通了

 public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(nums, path, result);
        return result;
    }

    void backtracking(int[] nums, List<Integer> path, List<List<Integer>> result) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int num : nums) {
            if (path.contains(num)) {
                continue;
            }
            path.add(num);
            backtracking(nums, path, result);
            path.remove(path.size() - 1);
        }
    }

47.全排列 II
重点在于如何去重,可以通过排序+used数组去重

 public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        backtracking1(nums, path, result, used);
        return result;
    }

    void backtracking1(int[] nums, List<Integer> path, List<List<Integer>> result, boolean[] used) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (used[i] || set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            used[i] = true;
            path.add(nums[i]);
            backtracking1(nums, path, result, used);
            path.remove(path.size() - 1);
            used[i] = false;
        }

    }

今日总结

  1. 这里有两种去重方式,一种是通过used数组去重,这种方法的关键是数组需要排序,还有一种是在for循环中用一个set去重;去重的关键是要分清树枝去重和树层去重,for循环代表一层,递归代表往下;
  2. 因为有公式的存在,很多题其实想不太明白,只是套公式,然后根据用例通不过的地方去修改,就解决了,而对于回溯本身想的不是很清楚,递归本身也比较抽象;
  3. 今天小绿,又追高了,真是愚蠢
  4. 另外,听说最近工作不好找,我还是得试试啊。
  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值