代码随想录算法训练营29期|day29 任务以及具体安排

* 491.递增子序列

java 复制代码
class Solution {
    List<List<Integer>>result = new ArrayList<>();
    LinkedList<Integer>path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> findSubsequences(int[] nums) {
        //Arrays.sort(nums);
        used = new boolean[nums.length];
        backTracking(nums, 0, used);
        return result;
    }

    public void backTracking(int[] nums, int startIndex, boolean[] used){
        if(path.size() >= 2){
            result.add(new ArrayList<>(path));
        }
        if(startIndex >= nums.length) return;
        HashSet<Integer>set = new HashSet<>();
        for(int i = startIndex ; i < nums.length ; i++){
            if(set.contains(nums[i] ) == true){
                continue;
            }
            if(!path.isEmpty() && nums[i] < path.getLast()){
                continue;
            }
            set.add(nums[i]);
            used[i] = true;
            path.add(nums[i]);
            backTracking(nums, i+1, used);
            path.removeLast();
            used[i] = false;            
        }
    }
}

思路:与子集问题类似,但是不能进行排序,因为需要挑出递增数列。进行树层去重和树枝去重,树枝去重需要比较path的最后一个和当前要加入的大小

* 46.全排列

java 复制代码
class Solution {

    List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
    LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0){
            return result;
        }
        used = new boolean[nums.length];
        permuteHelper(nums);
        return result;
    }

    private void permuteHelper(int[] nums){
        if (path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            permuteHelper(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}

思路:全排列问题相对简单,就是进行树枝去重,i每次从0开始,只要used[i] == true,就跳过。

复制代码
// 解法2:通过判断path中是否存在数字,排除已经选择的数字
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) return result;
        backtrack(nums, path);
        return result;
    }
    public void backtrack(int[] nums, LinkedList<Integer> path) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
        }
        for (int i =0; i < nums.length; i++) {
            // 如果path中已有,则跳过
            if (path.contains(nums[i])) {
                continue;
            } 
            path.add(nums[i]);
            backtrack(nums, path);
            path.removeLast();
        }
    }
}

解法二:通过判断path是否存在数字,排除已经选择的数字。

* 47.全排列 II

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer>path = new LinkedList<>();
    boolean[] used ;
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        used = new boolean[nums.length];
        Arrays.fill(used, false);
        backTracking(nums, used);
        return result;
    }

    public void backTracking(int[] nums, boolean[] used){
        if(path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0 ; i < nums.length ; i++){
            if(used[i] == true){
                continue;
            }
            if(i > 0 && nums[i] == nums[i-1] && used[i-1] == false){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backTracking(nums, used);
            used[i] = false;
            path.removeLast();

        }
    }
}

思路:在全排列的基础上增加了树层去重,先对nums数组进行排序,然后根据used进行树层去重,并且和全排列一样进行树枝去重。

相关推荐
爱睡懒觉的焦糖玛奇朵21 小时前
【从视频到数据集:焦糖玛奇朵的魔法工具使用说明】
人工智能·python·深度学习·学习·算法·yolo·音视频
Runawayliquor21 小时前
opbase:CANN 所有算子的公共地基
大数据·数据库·人工智能·算法
徐安安ye21 小时前
FlashAttention 为什么对序列长度这么“敏感”?
人工智能·算法
Java面试题总结1 天前
java高频面试题(2026最新)
java·开发语言·jvm·数据库·spring·缓存
黎阳之光1 天前
黎阳之光:以视频孪生重构智能监盘,为燃机打造新一代智慧电厂大脑
大数据·人工智能·算法·安全·数字孪生
苦逼的猿宝1 天前
学生心理咨询评估系统
java·毕业设计·springboot·计算机毕业设计
隔窗听雨眠1 天前
doctype、charset、meta如何控制整个渲染流水线
java·服务器·前端
绝知此事1 天前
【算法突围 02】树形结构与数据库索引:树形结构与数据库索引:从 BST 到 B+ 树的演化与 MySQL 优化
数据库·mysql·算法·面试·b+树
清木!1 天前
排序算法比较
数据结构·算法·排序算法
吴可可1231 天前
用Teigha修改并保存CAD文件
数据库·算法·c#