算法训练营Day29(回溯)

491.递增子序列

不能和之前去重一样,排序,然后i>startIndex&&nums[i]=nums[i-1]这样子了

491. 非递减子序列 - 力扣(LeetCode)

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {

        backTracking(nums,0);
        return result;
    }
    void backTracking(int [] nums,int startIndex){
        if(path.size()>=2){
            result.add(new ArrayList<>(path));
        }
        if(startIndex==nums.length){
            return;
        }
        HashSet<Integer> set = new HashSet<>();//每层for循环,去重
        for(int i = startIndex;i<nums.length;i++){
            //两个逻辑,递增和重复取数
            if(!path.isEmpty()&&nums[i]<path.get(path.size()-1) || set.contains(nums[i])){
                continue;//注意continue,是还可以继续for循环里面的
            }
            path.add(nums[i]);
            set.add(nums[i]);
            backTracking(nums,i+1);
            path.removeLast();
        }
    }
}

46.全排列

46. 全排列 - 力扣(LeetCode)

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        backTracking(nums);
        return result;
    }
    void backTracking(int [] nums){
        if(path.size()==nums.length){
            result.add(new ArrayList<>(path));
        }
        for(int i = 0;i<nums.length;i++){
            if(path.contains(nums[i])){
                continue;
            }
            path.add(nums[i]);
            backTracking(nums);
            path.removeLast();
        }
    }
}
java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean [] used;
    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        backTracking(nums);
        return result;
    }
    void backTracking(int [] nums){
        if(path.size()==nums.length){
            result.add(new ArrayList<>(path));
        }
        for(int i = 0;i<nums.length;i++){
            if(used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            backTracking(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}

47.全排列 II

47. 全排列 II - 力扣(LeetCode)

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

            //正式逻辑
            used[i] = true;
            path.add(nums[i]);
            backTracking(nums,used);
            used[i]=false;
            path.removeLast();
        }
    }
}
相关推荐
tankeven6 小时前
HJ156 走迷宫
c++·算法
旺仔.2916 小时前
STL排序算法详解
数据结构·算法·排序算法
美狐美颜sdk6 小时前
美颜SDK是什么?直播/短视频美颜SDK技术详解
人工智能·算法·美颜sdk·直播美颜sdk·美颜api
华农DrLai6 小时前
什么是远程监督?怎么自动生成训练数据?
人工智能·算法·llm·prompt·知识图谱
计算机安禾6 小时前
【数据结构与算法】第16篇:串(String)的定长顺序存储与朴素模式匹配
c语言·数据结构·c++·学习·算法·visual studio code·visual studio
AI科技星7 小时前
基于v≡c公设的理论优化方案
c语言·开发语言·算法·机器学习·数据挖掘
江不清丶7 小时前
垃圾收集算法深度解析:从标记-清除到分代收集的演进之路
java·jvm·算法
wanderist.7 小时前
从Nim游戏到SG函数
c++·算法·蓝桥杯
数据皮皮侠7 小时前
2285 上市公司组织衰退程度【Dec】2010-2024
大数据·人工智能·算法·制造
daxi1507 小时前
C语言从入门到进阶——第17讲:字符串函数
c语言·开发语言·算法·蓝桥杯