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


    491.递增子序列

    文字讲解递增子序列

    视频讲解递增子序列

    **状态:这题看了文字讲解才AC,掌握了如何在回溯里通过Set集合来对同层节点去重

    思路:

    代码:

    class Solution {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> tempList = new LinkedList<>();
        public List<List<Integer>> findSubsequences(int[] nums) {
            backTracking(nums, 0);
            return result;
        }
    
        //本题的关键在于,同层不能有重复元素,当前层的节点不能大于上一层的值
        public void backTracking(int[] nums, int startIndex) {
            if (startIndex>=nums.length) {
                return;
            }
            //借助set集合去重
            HashSet hs = new HashSet();
            for (int i = startIndex; i < nums.length; i++) {
                if ((!tempList.isEmpty() && tempList.get(tempList.size()-1) > nums[i]) || hs.contains(nums[i])) {
                    continue;
                }
                hs.add(nums[i]);
                tempList.offer(nums[i]);
                if (tempList.size()>1) {
                    result.add(new ArrayList<>(tempList));
                }
                backTracking(nums, i+1);
                tempList.pollLast();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    46.全排列

    文字讲解全排列

    视频讲解全排列

    状态:做完组合类的题,这题好简单

    思路:

    代码:

    class Solution {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> tempList = new LinkedList<>();
        boolean[] usedArr;
        public List<List<Integer>> permute(int[] nums) {
            this.usedArr = new boolean[nums.length];
            for (int i = 0; i < this.usedArr.length; i++) {
                this.usedArr[i] = false;
            }
            backTracking(nums);
            return result;
        }
    
        public void backTracking(int[] nums) {
            if (tempList.size()==nums.length) {
                //收集
                result.add(new ArrayList<>(tempList));
                return;
            }
            for (int i = 0; i < nums.length; i++) {
                if (usedArr[i]) {
                    continue;
                }
                usedArr[i]=true;
                tempList.offer(nums[i]);
                backTracking(nums);
                tempList.pollLast();
                usedArr[i]=false;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    47.全排列II

    文字讲解全排列II

    视频讲解全排列

    状态:将前两题的思路整合,这题ok

    思路:

    代码:

    class Solution {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> tempList = new LinkedList<>();
        boolean[] used;
        public List<List<Integer>> permuteUnique(int[] nums) {
            Arrays.sort(nums);
            this.used = new boolean[nums.length];
            for (int i = 0; i < used.length; i++) {
                used[i] = false;
            }
            backTracking(nums);
            return result;
        }
    
        public void backTracking(int[] nums) {
            if (tempList.size()==nums.length) {
                result.add(new ArrayList<>(tempList));
                return;
            }
            HashSet<Integer> hs = new HashSet();
            for (int i = 0; i < nums.length; i++) {
                if (used[i] || hs.contains(nums[i])) {
                    continue;
                }
                hs.add(nums[i]);
                used[i] = true;
                tempList.offer(nums[i]);
                backTracking(nums);
                tempList.pollLast();
                used[i] = false;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    ubuntu 18及以上版本配置IP的方法,你get了吗
    qt使用QWebEngineView加载百度地图失败
    IOI 2022国际信息学竞赛那些事儿(附Day1原题)
    Error running ‘xxx‘: Can‘t get remote credentials for deployment server XXX解决
    maven archetype 项目原型
    机器学习和深度学习领域的算法和模型
    Selenium 高级定位 Xpath
    Spring 源码(7)Spring的注解是如何解析的?
    C/C++读写二进制文件
    【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
  • 原文地址:https://blog.csdn.net/qq_40988139/article/details/137893200