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;
        }
    }
}
相关推荐
信奥卷王2 分钟前
[GESP202503 五级] 原根判断
java·数据结构·算法
兮山与3 分钟前
算法4.0
算法
nju_spy4 分钟前
力扣每日一题(二)任务安排问题 + 区间变换问题 + 排列组合数学推式子
算法·leetcode·二分查找·贪心·排列组合·容斥原理·最大堆
初听于你5 分钟前
高频面试题解析:算法到数据库全攻略
数据库·算法
翟天保Steven7 分钟前
ITK-基于Mattes互信息的二维多模态配准算法
算法
小咕聊编程10 分钟前
【含文档+源码】基于SpringBoot的过滤协同算法之网上服装商城设计与实现
java·spring boot·后端
代码对我眨眼睛10 分钟前
226. 翻转二叉树 LeetCode 热题 HOT 100
算法·leetcode·职场和发展
Zz_waiting.11 分钟前
Spring 原理
java·spring·spring自动管理
黑色的山岗在沉睡1 小时前
LeetCode 494. 目标和
算法·leetcode·职场和发展
haoly19894 小时前
数据结构和算法篇-线性查找优化-移至开头策略
数据结构·算法·移至开头策略