算法训练营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();
        }
    }
}
相关推荐
古城小栈21 小时前
Rust变量设计核心:默认不可变与mut显式可变的深层逻辑
算法·rust
电商API&Tina21 小时前
跨境电商 API 对接指南:亚马逊 + 速卖通接口调用全流程
大数据·服务器·数据库·python·算法·json·图搜索算法
LYFlied21 小时前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
长安er1 天前
LeetCode 20/155/394/739/84/42/单调栈核心原理与经典题型全解析
数据结构·算法·leetcode·动态规划·
MarkHD1 天前
智能体在车联网中的应用:第28天 深度强化学习实战:从原理到实现——掌握近端策略优化(PPO)算法
算法
能源系统预测和优化研究1 天前
【原创代码改进】考虑共享储能接入的工业园区多类型负荷需求响应经济运行研究
大数据·算法
yoke菜籽1 天前
LeetCode——三指针
算法·leetcode·职场和发展
小高不明1 天前
前缀和一维/二维-复习篇
开发语言·算法
bin91531 天前
当AI优化搜索引擎算法:Go初级开发者的创意突围实战指南
人工智能·算法·搜索引擎·工具·ai工具
曹牧1 天前
Java:Math.abs()‌
java·开发语言·算法