刷题第29天 | 491.递增子序列、46.全排列、47.全排列 II

491. Increasing Subsequences

题目链接:491. Increasing Subsequences
思路链接:代码随想录回溯算法-递增子序列

思路

在这里插入图片描述
回溯算法:每次递归都要利用set来记录本层递归使用的元素,到下一层set就会被重置。如果本层遍历到一个已经被使用的元素,将会被跳过continue。可以用数组来做哈希表能进一步优化代码。另外如果path最后一个值大于当前遍历到的元素,也直接跳过continue。其他就是找子集的正常回溯写法。

Code

class Solution {
    private List<List<Integer>> result = new LinkedList<>();
    private List<Integer> path = new LinkedList<>();
    
    private void backtracking(int[] nums, int startIndex) {
        if (path.size() > 1) {
            result.add(new LinkedList<>(path));
        }
        Set<Integer> uset = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            if (!path.isEmpty() && path.get(path.size() - 1) > nums[i] || uset.contains(nums[i])) {
                continue;
            }
            uset.add(nums[i]);
            path.add(nums[i]);
            backtracking(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
    
    public List<List<Integer>> findSubsequences(int[] nums) {
        backtracking(nums, 0);
        return result;
    }
}

46. Permutations

题目链接:46. Permutations
思路链接:代码随想录回溯算法-全排列

思路

还是比较简单,需要使用used数组来记录本树支已经使用的元素,从而防止重复使用元素的问题。另外因为本体是求排列,每次递归都需要遍历整个数组,配合used数组来除去重复元素。
在这里插入图片描述

Code

class Solution {
    private List<Integer> path = new LinkedList<>();
    private List<List<Integer>> result = new LinkedList<>();
    
    
    private void backtracking(int[] nums, int[] used) {
        // 结束条件,当path长度等于数组长度时结束
        if (path.size() == nums.length) {
            result.add(new LinkedList<>(path));
            return;
        }
        // 因为我们要取排列,所以每个元素都要取到,因此每个递归i要从0开始
        for (int i = 0; i < nums.length; i++) {
            // 遇到已经在path中的元素
            if (used[i] == 1) {
                continue;
            }
            used[i] = 1;
            path.add(nums[i]);
            backtracking(nums, used);
            path.remove(path.size() - 1);
            used[i] = 0;
        }
    }
    
    public List<List<Integer>> permute(int[] nums) {
        int[] used = new int[nums.length];
        backtracking(nums, used);
        return result;
    }
}

47. Permutations II

题目链接:47. Permutations II
思路链接:代码随想录回溯算法-全排列II

思路

在这里插入图片描述
因为这道题数组中会出现重复元素,因此需要借助used数组来去重(树丛去重),但前提需要将数组先排序。注意在遍历时,因为是取排列,每次开始都是从nums[0]开始。另外,还需配合used数组除去重复使用的元素。

Code

class Solution {
    private List<Integer> path = new LinkedList<>();
    private List<List<Integer>> result = new LinkedList<>();
    
    private void backtracking(int[] nums, int[] used) { 
        if (path.size() == nums.length) {
            result.add(new LinkedList<>(path));
            return;
        }
        // 因为取排列,每次从0开始
        for (int i = 0; i < nums.length; i++) {
            // 遇到重复元素,树支上可以取重复,树丛上不可以取重复
            // 树丛去重
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0) { // 使用used数组前提要将输入数组排序
                continue;
            }
            // 除去已经在path中的元素,防止重复使用元素
            if (used[i] == 0) {
                path.add(nums[i]);
                used[i] = 1;
                backtracking(nums, used);
                path.remove(path.size() - 1);
                used[i] = 0;
            }
        }
    }
    
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        int[] used = new int[nums.length];
        backtracking(nums, used);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值