代码随想录刷题|LeetCode 491.递增子序列 46.全排列 47.全排列II

简介: 代码随想录刷题|LeetCode 491.递增子序列 46.全排列 47.全排列II

491.递增子序列

题目链接:力扣

思路


   这道题目不能排序, 因为要保证目前原数组中的递归的子数组

       数组中存在重复的元素,所以需要对树层进行去重

       因为不能排序,所以无法使用used数组进行去重


       要做到能收集数组中的所有的情况,要做到两点:

               1、当一条树枝中后面的元素不满足递增的情况,需要跳过,继续收集后面的元素

              bcad2abe7d1f4685903170786ea587b7.png

               2、当树层中出现中已经使用过的数字时,要跳过,继续收集后面的元素,因为不能排序,所以需要使用set集合记录这层树层中出现过的元素

               这个set集合不用跟着回溯,因为就是记录这层的元素出现情况

               每进一层递归都会创建一个新的set集合

              a84a29be4e6c4804a9084fbf2c3aaf53.png


        需要注意的一点是收集结果的条件:

               形成递增至少需要两个元素,所以需要 path.size() > 1

              8d72aa6a2b14448b93e0ced8b14aea45.png


递增子序列

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        backtracking(nums,0);
        return result;
    }
    void backtracking(int[] nums, int startIndex) {
        if (path.size() > 1) {
            result.add(new ArrayList<>(path));
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            // 不满足递增的情况
            if (!path.isEmpty() && nums[i] < path.getLast()) {
                continue;
            }
            // 使用过了的数字
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            path.add(nums[i]);
            backtracking(nums,i+1);
            path.removeLast();
        }
    }
}

 

46.全排列

题目链接:力扣

思路


   这道题目中没有重复的元素,所以不用考虑去重的问题,因为是排列问题,所以递归中的每一次循环都需要从第一个元素进行处理,但是这样就会出现添加同一元素的问题,所以需要对这样情况进行处理

3c19b187d0f042a293af5fde4390783c.png

       在下面排列的数结构图进行分析


37bda83db9fc48fc88f99f18f03dc03c.png


  再有一种思路就是使用used数组进行树枝去重,使用used数组标记哪个元素已经使用过了

全排列

通过path排除

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>(); 
    public List<List<Integer>> permute(int[] nums) {
        backtracking(nums);
        return result;      
    }
    void backtracking( int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {
            if (path.contains(nums[i])) {
                continue;
            } else {
                path.add(nums[i]);
                backtracking(nums);
                path.removeLast();
            } 
        }
    }
}       

通过used[]数组排除

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

47.全排列 II

题目链接:力扣

思路


     因为集合中有重复的元素,不仅要对树层进行去重,也要对树枝进行去重,最好的选择就是使用used数组,所以这道题目关键就是在去重


       path只适合对树枝进行去重,因为path只作用于一条树枝


       nums[i] == nums[i-1] && used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过

       nums[i] == nums[i-1] && used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过


全排列||

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used,false);
        backstarking(nums,used);
        return result;
    }
    void backstarking(int[] nums,boolean[] used){
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            // used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
            // used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
            // 如果同⼀树层nums[i - 1]使⽤过则直接跳过
            // used数组避免同树枝上的相同元素被去重
            if (i > 0 && nums[i] == nums[i-1] && used[i - 1] == false) {
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理
            //如果对应的used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
            if (used[i] == false) {
                path.add(nums[i]);
                used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
                backstarking(nums,used);
                path.removeLast();//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
                used[i] = false;
            }
        }
    }
}
相关文章
|
3天前
|
机器学习/深度学习
leetcode代码记录(旋转图像
leetcode代码记录(旋转图像
8 0
|
3天前
|
算法
leetcode代码记录(全排列 II
leetcode代码记录(全排列 II
9 4
|
3天前
|
算法
leetcode代码记录(全排列
leetcode代码记录(全排列
9 1
|
3天前
|
索引
leetcode代码记录(Z 字形变换
leetcode代码记录(Z 字形变换
7 1
|
3天前
leetcode代码记录(最长回文子串
leetcode代码记录(最长回文子串
7 2
|
3天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
7 0
|
3天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
8 0
|
4天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
21 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
4天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩

热门文章

最新文章