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

文章目录

491.递增子序列

文字讲解递增子序列

视频讲解递增子序列

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

思路:

代码:

java 复制代码
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.全排列

文字讲解全排列

视频讲解全排列

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

思路:

代码:

java 复制代码
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

思路:

代码:

java 复制代码
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;
        }
    }
}
相关推荐
我叫小白菜6 分钟前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
森焱森11 分钟前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
Albert Edison39 分钟前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍1 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122461 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~2 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇2 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097622 小时前
JavaEE-Linux环境部署
java·linux·java-ee
QuantumStack2 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
缘来是庄2 小时前
设计模式之访问者模式
java·设计模式·访问者模式