算法训练营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();
        }
    }
}
相关推荐
earthzhang202130 分钟前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
2301_803554522 小时前
C++联合体(Union)详解:与结构体的区别、联系与深度解析
java·c++·算法
sali-tec3 小时前
C# 基于halcon的视觉工作流-章42-手动识别文本
开发语言·人工智能·算法·计算机视觉·c#·ocr
SandySY3 小时前
品三国谈人性
算法·架构
小欣加油4 小时前
leetcode 62 不同路径
c++·算法·leetcode·职场和发展
夏鹏今天学习了吗4 小时前
【LeetCode热题100(38/100)】翻转二叉树
算法·leetcode·职场和发展
夏鹏今天学习了吗4 小时前
【LeetCode热题100(36/100)】二叉树的中序遍历
算法·leetcode·职场和发展
DTS小夏4 小时前
算法社Python基础入门面试题库(新手版·含答案)
python·算法·面试
Mr.Ja4 小时前
【LeetCode热题100】No.11——盛最多水的容器
算法·leetcode·贪心算法·盛水最多的容器
冷徹 .4 小时前
2024ICPC区域赛香港站
数据结构·c++·算法