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;
        }
    }
}
相关推荐
yq1982043011566 小时前
静思书屋:基于Java Web技术栈构建高性能图书信息平台实践
java·开发语言·前端
一个public的class6 小时前
你在浏览器输入一个网址,到底发生了什么?
java·开发语言·javascript
有位神秘人6 小时前
kotlin与Java中的单例模式总结
java·单例模式·kotlin
golang学习记6 小时前
IntelliJ IDEA 2025.3 重磅发布:K2 模式全面接管 Kotlin —— 告别 K1,性能飙升 40%!
java·kotlin·intellij-idea
爬山算法6 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
java·压力测试·hibernate
仟濹6 小时前
算法打卡day2 (2026-02-07 周五) | 算法: DFS | 3_卡码网99_计数孤岛_DFS
算法·深度优先
驭渊的小故事6 小时前
简单模板笔记
数据结构·笔记·算法
YuTaoShao6 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法一)前后缀分解
算法·leetcode·职场和发展
消失的旧时光-19436 小时前
第十四课:Redis 在后端到底扮演什么角色?——缓存模型全景图
java·redis·缓存
BD_Marathon6 小时前
设计模式——依赖倒转原则
java·开发语言·设计模式