海智算法训练营第二十九天 | 第七章 回溯算法part05 | * 491.递增子序列* 46.全排列* 47.全排列 II

news/2024/7/20 21:28:43 标签: 深度优先, 算法

今日目标:

1.查找所有的非递减子序列

2.全排列

3.全排列II

1.查找所有的非递减子序列

题目:非递减子序列

原理:这道题不能用之前那种去重的方法,因为那些需要有序,这样只需要比较前后两个的值是否相同,而这道题不能使用排序,因为题目的要求是以当前序列的顺序找出他的非递减子序列。

例如:

  • 输入: [4, 6, 7, 7]
  • 输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

所以,我们引用了set集合来充当uset数组来记录是否使用过这个数,也能达到理想的效果。

注意点:结束递归的出口是当path的长度大于等于二的时候,因为序列要求至少有两个数 

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        dfs(nums,0);
        return res;
    }
    public void dfs(int[] nums , int start){
        if(path.size() >= 2)    res.add(new ArrayList<>(path));
        if(start > nums.length -1)  return;
        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]);
            dfs(nums,i+1);
            path.remove(path.size()-1);
        }
    }
}

2.全排列

题目:全排列

做法:这里我和题解不同的地方是我用的是hash集合来去重,感觉代码比较简单就没有看视频学习了。

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    HashSet<Integer> hs = new HashSet<>();
    public List<List<Integer>> permute(int[] nums) {
        dfs(nums);
        return res;
    }

    public void dfs(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        
        for (int i = 0; i < nums.length; i++) {
            if(hs.contains(nums[i]))    continue;
            path.add(nums[i]);
            hs.add(nums[i]);
            dfs(nums);
            path.remove(path.size()-1);
            hs.remove(nums[i]);
        }
}
}

3.全排列II

题目:全排列II

class Solution {
    //存放结果
    List<List<Integer>> result = new ArrayList<>();
    //暂存结果
    List<Integer> path = new ArrayList<>();

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

    private void backTrack(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]使⽤过则直接跳过
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理
            if (used[i] == false) {
                used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
                path.add(nums[i]);
                backTrack(nums, used);
                path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
                used[i] = false;//回溯
            }
        }
    }
}

学习时长:3h

总结:更加熟悉了回溯的去重做法,以及了解了全排列的相关题目做法


http://www.niftyadmin.cn/n/5390855.html

相关文章

LeetCode 0106.从中序与后序遍历序列构造二叉树:分治(递归)——五彩斑斓的题解(若不是彩色的可以点击原文链接查看)

【LetMeFly】106.从中序与后序遍历序列构造二叉树&#xff1a;分治&#xff08;递归&#xff09;——五彩斑斓的题解&#xff08;若不是彩色的可以点击原文链接查看&#xff09; 力扣题目链接&#xff1a;https://leetcode.cn/problems/construct-binary-tree-from-inorder-an…

vue3 globalData 的使用方法

直接上教程 .新创建一个data.js它可以和main.js平级也可以在store文件夹下面都行&#xff0c;无非就是引用的时候前缀多一个单词少一个单词这样 data.js: 从vue上面引入reactive &#xff0c;然后它可以创建一个也可以创建两个可以任意名称 A或B或C都可以 //data.js import…

如何让家中的工作站提提网速

最近一直很好奇&#xff0c;我的Arch工作站在下载huggingface model时总是在5MB/s&#xff0c; 而我的Win10笔记本却可以上10MB/s。经过我的发现时由于使用的wifi 频段 2.4G 和 5G 的区别。 什么是wifi 2.4G 和 5G 2.4G和5G是指无线网络的工作频率&#xff0c;其中2.4G指的是…

加密货币与区块链误解释疑

银本位、金本位的时代已经成为历史。货币&#xff0c;必须有国家信用的背书&#xff0c;否则&#xff0c;就是废纸。加密货币&#xff0c;只能应用于虚拟世界。 加密技术丰富而且成熟&#xff0c;区块链技术&#xff0c;只是在现有加密技术之上&#xff0c;为解决特定问题而有的…

@Resource注入和@Autowired注入有什么区别

Resource注解也可以完成属性注入&#xff0c;那它和Autowired注解有什么区别&#xff1f; 1.Resource注解是jdk扩展包中的&#xff0c;也就是说属于JDK的一部分。所以该注解是标准的注解&#xff0c;更加具有通用性。 2.Autowired注解是Spring框架自己的。 3.Resource注解默…

gem5学习(24):缓存一致性协议——Cache Coherence Protocols

目录 一、Common Notations and Data Structures 1、Coherence Messages 2、Access Permissions 3、Data Structures 二、Coherence controller FSM Diagrams 官网教程&#xff1a;gem5: Cache Coherence Protocols 一、Common Notations and Data Structures &#xff…

突破编程_C++_面试(函数(1))

面试题1&#xff1a;函数定义与声明有什么区别&#xff1f; 函数定义与声明的区别主要体现在以下几个方面&#xff1a; 内存分配&#xff1a; 定义&#xff1a;函数定义会为函数分配内存空间&#xff0c;并且可能会为函数内部的局部变量分配内存。定义提供了函数在程序中的唯一…

【Numpy】P1 概述与安装

目录 概述安装Windows MacOS 环境下Linux 环境下 概述 NumPy&#xff08;Numerical Python&#xff09;&#xff0c;是一个对 Python 进行增强的库。它为 Python 提供了强大的多维数组对象和丰富的数学函数库&#xff0c;主要用于处理高维数组和矩阵计算。其核心功能包括&…