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;
        }
    }
}
相关推荐
找不到、了32 分钟前
分布式理论:CAP、Base理论
java·分布式
天天摸鱼的java工程师35 分钟前
2025已过半,Java就业大环境究竟咋样了?
java·后端
运器12337 分钟前
【一起来学AI大模型】支持向量机(SVM):核心算法深度解析
大数据·人工智能·算法·机器学习·支持向量机·ai·ai编程
人生在勤,不索何获-白大侠39 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
Zedthm1 小时前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode
艺杯羹1 小时前
MyBatis之核心对象与工作流程及SqlSession操作
java·mybatis
神的孩子都在歌唱1 小时前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
YuTaoShao1 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法一)空间复杂度 O(M + N)
算法·leetcode·矩阵
喜欢吃豆2 小时前
深入企业内部的MCP知识(三):FastMCP工具转换(Tool Transformation)全解析:从适配到增强的工具进化指南
java·前端·人工智能·大模型·github·mcp
用户1551733938832 小时前
前后端处理 `multipart/form-data` 混合参数(实体对象+文件)方案
java