(回溯05) 非递减子序列 全排列 全排列II

一、非递减子序列

力扣第491题

该题新加入 set 集合用来去重,判断当前层是否有重复元素

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> sequence = new ArrayList<>();
    int count = 0;
    public List<List<Integer>> findSubsequences(int[] nums) {
        
        backtracking(nums, 0);
        return result;
    }

    public void backtracking(int[] nums, int index) {
        Set<Integer> set = new HashSet<>();
        if(index == nums.length) return;

        for(int i = index; i < nums.length; i++) {
            
            if(!set.add(nums[i])) {
                continue;
            }
            if(count != 0 && nums[i] < sequence.get(sequence.size() - 1)) {
                continue;
            }
            sequence.add(nums[i]);
            count++;
            if(count >= 2) {
                result.add( new ArrayList(sequence));
            }
            backtracking(nums, i + 1);
            sequence.remove(sequence.size() - 1);
            count--;
        }
    }
}

时间复杂度:O(n*)

空间复杂度:O(n)

二、全排列

力扣第46题

利用数组去掉已经选出来的元素

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used = new int[21];
    public List<List<Integer>> permute(int[] nums) {
        backtracking(nums);
        return result;
    }
    public void backtracking(int[] nums) {
        if(path.size() == nums.length) {
            result.add(new ArrayList(path));
            return;
        }

        for(int i = 0; i < nums.length; i++) {
            if(used[nums[i] + 10] == 1) {
                continue;
            }
            used[nums[i] + 10] = 1;
            path.add(nums[i]);
            backtracking(nums);
            path.remove(path.size() - 1);
            used[nums[i] + 10] = 0;
        }
    }
}

时间复杂度:O(n!)

空间复杂度:O(n)

三、全排列 II

力扣第47题

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used = new int[21];
    public List<List<Integer>> permute(int[] nums) {
        backtracking(nums);
        return result;
    }
    public void backtracking(int[] nums) {
        if(path.size() == nums.length) {
            result.add(new ArrayList(path));
            return;
        }

        for(int i = 0; i < nums.length; i++) {
            if(used[nums[i] + 10] == 1) {
                continue;
            }
            used[nums[i] + 10] = 1;
            path.add(nums[i]);
            backtracking(nums);
            path.remove(path.size() - 1);
            used[nums[i] + 10] = 0;
        }
    }
}

时间复杂度:O(n! * n)

空间复杂度:O(n)

相关推荐
漫随流水2 分钟前
leetcode算法(513.找树左下角的值)
数据结构·算法·leetcode·二叉树
囊中之锥.37 分钟前
机器学习算法详解:DBSCAN 聚类原理、实现流程与优缺点分析
算法·机器学习·聚类
AlenTech1 小时前
152. 乘积最大子数组 - 力扣(LeetCode)
算法·leetcode·职场和发展
Piar1231sdafa1 小时前
基于yolo13-C3k2-RVB的洗手步骤识别与检测系统实现_1
人工智能·算法·目标跟踪
做科研的周师兄1 小时前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
天赐学c语言1 小时前
1.18 - 滑动窗口最大值 && 子类的指针转换为父类的指针,指针的值是否会改变
数据结构·c++·算法·leecode
甄心爱学习2 小时前
KMP算法(小白理解)
开发语言·python·算法
wen__xvn2 小时前
牛客周赛 Round 127
算法
大锦终2 小时前
dfs解决FloodFill 算法
c++·算法·深度优先
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 200 题:岛屿数量
算法·leetcode·职场和发展