算法训练营Day29(回溯)

491.递增子序列

不能和之前去重一样,排序,然后i>startIndex&&numsi=numsi-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();
        }
    }
}
相关推荐
xunberg4 分钟前
【生命科学 Agent】推理平台(三)
算法·程序员·架构
INGNIGHT4 分钟前
151 · 买卖股票的最佳时机 III(序列dp)
数据结构·算法·leetcode
庖丁解牛8 分钟前
十进制转二进制:凑数法(填1法)
算法
行者全栈架构师15 分钟前
【共创稿事节】双引擎智能相册:HarmonyOS 7(API 26)文搜图 × 图像超分端侧 AI 联动实战
前端·算法·架构
小蒋观天下34 分钟前
【无标题】红外补光近距过曝、远距欠曝:导致车牌AI识别失效调试
人工智能·深度学习·数码相机·算法·机器学习·ai大模型
吞下星星的少年·-·2 小时前
牛客周赛 119B题(贪心)
算法·贪心
NEFU_Jyj2 小时前
牛客周赛R166(max型瓶颈最短路,前后缀DP合并)
算法·深度优先
Tisfy2 小时前
LeetCode 3644.排序排列:不对则与(脑筋急转弯)
数据结构·算法·leetcode·题解·位运算
lisw053 小时前
分岔论揭示演化突变密码
开发语言·算法·数学建模
aqiu1111114 小时前
C++ TopK:使用 nth_element 快速求最小 K 个数(蓝桥杯 / LeetCode 可用)
算法