算法训练营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();
        }
    }
}
相关推荐
binnnngo30 分钟前
Minmax 算法与 Alpha-Beta 剪枝小教学
算法·机器学习·剪枝
এ᭄画画的北北2 小时前
力扣-287.寻找重复数
算法·leetcode
YuTaoShao9 小时前
【LeetCode 热题 100】141. 环形链表——快慢指针
java·算法·leetcode·链表
小小小新人1212310 小时前
C语言 ATM (4)
c语言·开发语言·算法
你的冰西瓜11 小时前
C++排序算法全解析(加强版)
c++·算法·排序算法
এ᭄画画的北北11 小时前
力扣-31.下一个排列
算法·leetcode
绝无仅有12 小时前
企微审批对接错误与解决方案
后端·算法·架构
用户50408278583913 小时前
1. RAG 权威指南:从本地实现到生产级优化的全面实践
算法
Python×CATIA工业智造14 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm