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();
        }
    }
}

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;
        }
    }
}

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. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 10:58:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 10:58:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 10:58:01       87 阅读
  4. Python语言-面向对象

    2024-04-21 10:58:01       96 阅读

热门阅读

  1. 发票查验详情、C票据识别、发票ocr

    2024-04-21 10:58:01       32 阅读
  2. OCR API、文字识别接口、文字录入

    2024-04-21 10:58:01       36 阅读
  3. RTT设备驱动框架学习(GPIO)

    2024-04-21 10:58:01       39 阅读
  4. 23种设计模式之创建型模式篇

    2024-04-21 10:58:01       28 阅读
  5. 解惑深度学习中的困惑度Perplexity

    2024-04-21 10:58:01       38 阅读
  6. 《设计模式之美》第二章总结

    2024-04-21 10:58:01       37 阅读
  7. 【Node.js】Node.js的安装与配置

    2024-04-21 10:58:01       33 阅读